首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我不确定我的分区函数出了什么问题

我不确定我的分区函数出了什么问题
EN

Stack Overflow用户
提问于 2013-04-25 01:31:28
回答 1查看 179关注 0票数 0

Eclipse一直告诉我我的分区函数有问题。它是用java编写的,是一个关于数组排序的类的一部分。

分区是这样工作的:数组中有两个索引i和j,在分区算法的最开始,i指向数组中的第一个元素,j指向最后一个元素。然后,算法向前移动i,直到找到值大于或等于枢轴的元素。索引j向后移动,直到找到值小于或等于透视的元素。如果i≤j,那么它们被交换,并且i步到下一个位置(i + 1),j步到前一个位置(j - 1)。当i大于j时,算法停止。

你能看到问题出在哪里吗,因为我很难找到它。如果您能帮上忙,我们将不胜感激。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public static int partition(int arr[], int left, int right)
    {
        int x = arr[right];

        int i = left-1;
        int temp=0;

        for (int j=left; j<right; j++)
        {
            if(arr[j]<=x)
            {
                i++;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;

            }
        }

        temp = arr[i+1];
        arr[i+1] = arr[right];
        arr[right] = temp;
        return (i+1);

    }

编辑

Eclipse说有两个问题,一个是分区函数中的这行代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int x = arr[right];

使用我的测试类中的这一行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
sort.partition(array, 100, array.length);

下面是测试类,其中包含我没有提到的函数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.util.Random;


public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int size = 1000;
        int max = 5000; 
        int[] array = new int[size];
        int loop = 0; 

        Random generator = new Random();
        //Write a loop that generates 1000 integers and 
        //store them in the array using generator.nextInt(max)

        generator.nextInt(max); //generating one

        //I need to generate 1000
        //So I need some kind of loop that will generate 1000 numbers. 

        for (int i =0; i<1000; i++)
        {
            generator.nextInt(max);
        }



        /**
         * After I do this, I'll have the array, array. 
         * Then comes what's under this. 
         * THat method is for measuring the time.
         * System.currentTimeMillis();, 
         * with this, I can collect a time for the start of the method
         * and one for the end. 
         * Time at the end, minus the time at the start
         * gets us the running time. 
         */



        long result;

        long startTime = System.currentTimeMillis();
        sort.quickSort(array,  100,  array.length-1);
        long endTime = System.currentTimeMillis();
        result = endTime-startTime; 

        System.out.println("The quick sort runtime is " + result + " miliseconds");

        long result2;

        long startTime2 = System.currentTimeMillis(); 
        sort.partition(array, 100, array.length);
        long endTime2 = System.currentTimeMillis();
        result2 =  endTime2 - startTime2;
        System.out.println("The partition runtime is "+result2 + " miliseconds");

        long result3;

        long startTime3 = System.currentTimeMillis();
        sort.bubbleSort(array, 100);
        long endTime3 = System.currentTimeMillis();
        result3 = endTime3-startTime3;
        System.out.println("The bubble sort runtime is "+result3 + " miliseconds");

        long result4;

        long startTime4 = System.currentTimeMillis();
        sort.selectionSort(array, 100); //change the second number to change
        //the size of an array. 
        long endTime4 = System.currentTimeMillis();
        result4 = endTime4-startTime4;
        System.out.println("The selection sort runtime is "+result4 + " miliseconds");



    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-25 02:31:06

首先,您实际上并没有在数组中存储随机数,所以它将是全零的。

至于你看到的实际错误,你已经得到了一个错误的经典错误。您有两个选择:让right指向数组的末尾,或者指向数组的末尾。其中任何一个都是有效的,但是您将这两个混合在一起。

具体地说,您为right值传递了1000 (超过数组末尾一次),但随后您立即用它对数组进行索引,自然地抛出了异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16204979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文