Java I';我不确定是什么';我的配分函数有什么问题吗
Eclipse一直告诉我我的分区函数有问题。它是用java编写的,是一个类的一部分,这个类的全部内容都是关于数组排序的 分区的工作原理如下:数组中有两个索引i和j,在分区算法的最开始,i指向数组中的第一个元素,j指向最后一个元素。然后算法向前移动i,直到找到值大于或等于枢轴的元素。索引j向后移动,直到找到值小于或等于枢轴的元素。如果我≤ 然后它们交换,i步到下一个位置(i+1),j步到上一个位置(j-1)。当i大于j时,算法停止 你能看出问题出在哪里吗,因为我找不到它。非常感谢您的帮助Java I';我不确定是什么';我的配分函数有什么问题吗,java,debugging,sorting,Java,Debugging,Sorting,Eclipse一直告诉我我的分区函数有问题。它是用java编写的,是一个类的一部分,这个类的全部内容都是关于数组排序的 分区的工作原理如下:数组中有两个索引i和j,在分区算法的最开始,i指向数组中的第一个元素,j指向最后一个元素。然后算法向前移动i,直到找到值大于或等于枢轴的元素。索引j向后移动,直到找到值小于或等于枢轴的元素。如果我≤ 然后它们交换,i步到下一个位置(i+1),j步到上一个位置(j-1)。当i大于j时,算法停止 你能看出问题出在哪里吗,因为我找不到它。非常感谢您的帮助 publ
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);
}
在我的测试课上:
sort.partition(array, 100, array.length);
这里是测试类,它包括我没有提到的函数
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");
}
}
import java.util.Random;
公开课考试{
/**
*@param args
*/
公共静态void main(字符串[]args){
int size=1000;
int max=5000;
int[]数组=新的int[size];
int循环=0;
随机生成器=新随机();
//写一个循环,生成1000个整数和
//使用generator.nextInt(max)将它们存储在阵列中
generator.nextInt(max);//正在生成一个
//我需要生成1000个
//所以我需要一种循环,可以产生1000个数字。
对于(inti=0;i首先,实际上并没有将随机数存储在数组中,因此它将全部为零
至于您看到的实际错误,您有一个经典错误。您有两个选择:使向右
指向数组的末尾或一个指向数组的末尾。其中一个是有效的,但您混合了这两个
具体地说,对于right
值,你要传递1000,超过数组末尾一个,但是你立即用它索引数组,自然会抛出一个异常。Eclipse说它有什么问题?你可以找到一些工作示例。我再次运行它,它说:我没有意识到按enter键会发布commont、 这里:线程“main”java.lang.ArrayIndexOutOfBoundsException:1000 at sort.partition(sort.java:48)at test.main(test.java:59)中的异常不要紧。多亏了你的建议,我解决了这个问题并消除了错误,但你对我的数组是空的说法也是正确的。不过,谢谢你的帮助。
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");
}
}