我已经写了这段代码,但它将在控制台中打印这些堆栈跟踪,请帮助我,谢谢!( "p“和"q”分别是数组的第一个和最后一个索引)
public class JavaQuickSort {
public static void QuickSort(int A[], int p, int q) {
int i, last = 0;
Random rand = new Random();
if (q < 1) {
return;
}
**swap(A, p, rand.nextInt() % (q+1));**
for (i = p + 1; i <= q; i++) {
if (A[i] < A[p]) {
swap(A, ++last, i);
}
}
swap(A, p, last);
QuickSort(A, p, last - 1);
QuickSort(A, last + 1, q);
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
**A[i] = A[j];**
A[j] = temp;
}
public static void main(String[] args){
int[] A = {2,5,7,3,9,0,1,6,8};
**QuickSort(A, 0,8 );**
System.out.println(Arrays.toString(A));
}
}堆栈跟踪:
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -3
at JavaQuickSort.swap(JavaQuickSort.java:38)
at JavaQuickSort.QuickSort(JavaQuickSort.java:22)
at JavaQuickSort.main(JavaQuickSort.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)我还对导致堆栈跟踪的语句进行了加粗处理。像==> ** ...**
编辑:
public class JavaQuickSort {
public static void QuickSort(int arr[],int lo,int hi) {
int n = arr.length;
if(n<=1)
return;
**int r = partition(arr);**
**QuickSort(arr,lo , r-1);**
QuickSort(arr, r+1, hi);
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
**A[i] = A[j];**
A[j] = temp;
}
public static int partition(int arr[]){
int i, last = 0;
Random rand = new Random();
int n = arr.length;
if (n <= 1)
return arr[0];
**swap(arr, 0, rand.nextInt(n));**
for (i =1; i <n; i++) {
if (arr[i] < arr[0]) {
swap(arr, ++last, i);
}
}
swap(arr, 0, last);
return last;
}
public static void main(String[] args){
int[] A = {2,5,7,3,9,0,1,6,8};
**QuickSort(A, 0,8 );**
System.out.println(Arrays.toString(A));
}
}我编辑我的帖子是为了更容易理解,它也会打印这些堆栈跟踪,我加粗了导致这些堆栈跟踪的行!
堆栈跟踪:
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at JavaQuickSort.swap(JavaQuickSort.java:27)
at JavaQuickSort.partition(JavaQuickSort.java:39)
at JavaQuickSort.QuickSort(JavaQuickSort.java:19)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
at JavaQuickSort.main(JavaQuickSort.java:52)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)请帮帮我谢谢
编辑:
我写了这段代码,注意到了这篇文章的第一个答案,但它不会对我的数组进行排序!
public class JavaQuickSort {
public static void QuickSort(int arr[], int lo, int hi) {
if (hi > lo) {
Random rand = new Random();
int pivotIndex = lo + rand.nextInt(hi-lo+1);
int pivotnewIndex = partition(arr, lo, hi,pivotIndex);
QuickSort(arr, lo, pivotnewIndex - 1);
QuickSort(arr, pivotnewIndex + 1, hi);
}
}
private static void swap(int[] A, int i, int j) {
int temp;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public static int partition(int arr[],int lo,int hi,int pivotIndex)
{
int pivotValue = arr[pivotIndex];
swap(arr, hi, pivotIndex);
int storeIndex = lo;
for(int i = lo;i<hi;i++){
if (arr[i]<=pivotValue)
swap(arr, storeIndex, i);
storeIndex = storeIndex ++;
}
swap(arr, storeIndex, hi);
return storeIndex;
}
public static void main(String[] args) {
int[] A = {2, 5, 7, 3, 9, 0, 1, 6, 8};
QuickSort(A, 0, 8);
System.out.println(Arrays.toString(A));
}
}输出:
run:
[2, 9, 3, 8, 0, 6, 7, 1, 5]
BUILD SUCCESSFUL (total time: 2 seconds)我需要你的帮助,我真的很困惑!
发布于 2010-06-08 15:51:41
您的代码存在以下问题:
Random实例。只需创建一次,将其存储在比方说static字段中,然后使用它为您的rangep和q之间。lo和hi生成随机数使用p和hi而不是p和D16,并且D17而不是D16可以使用其成员D21获取数组的长度至于快速排序算法本身,它看起来不像我以前见过的任何东西。我建议研究标准的命令式实现,并对其进行调整。
另请参阅
更新
不幸的是,您在某种程度上混淆了维基百科上的伪代码。您想要调整此算法:
function partition(array, left, right, pivotIndex)
pivotValue := array[pivotIndex]
swap array[pivotIndex] and array[right] // Move pivot to end
storeIndex := left
for i from left to right - 1 // left ≤ i < right
if array[i] ≤ pivotValue
swap array[i] and array[storeIndex]
storeIndex := storeIndex + 1
swap array[storeIndex] and array[right] // Move pivot to its final place
return storeIndex
procedure quicksort(array, left, right)
if right > left
select a pivot index //(e.g. pivotIndex := left+(right-left)/2)
pivotNewIndex := partition(array, left, right, pivotIndex)
quicksort(array, left, pivotNewIndex - 1)
quicksort(array, pivotNewIndex + 1, right)请注意,上面的算法选择left和right之间的子数组的中间元素作为透视索引。由于您需要随机化的快速排序,因此需要在left和right之间选择一个随机索引,因此需要按如下方式更改公式:
pivotIndex := left + (random number between 0 and right-left inclusive)例如,如果是left = 5和right = 7,那么您希望pivotIndex为5 + x,其中x是介于0和7-5=2之间的随机数。
因为Random.nextInt(n)有一个排他性的上限,所以把它翻译成Java应该是这样的:
int pivotIndex = lo + rand.nextInt(hi - lo + 1);最终修正
你犯了一个初学者常犯的错误:
// HORRIBLE FORMATTING! Don't do this!
if (arr[i]<=pivotValue)
swap(arr, storeIndex, i);
storeIndex = storeIndex ++;如果您注意到上面的伪代码,这两个语句都应该是if主体的一部分,但如果正确缩进并添加大括号,上面的代码实际上是这样的:
// PROPER FORMATTING! Reveals bug instantly!
if (arr[i]<=pivotValue) {
swap(arr, storeIndex, i);
}
storeIndex = storeIndex ++;因此,修复方法是将storeIndex增量移动到if主体,如下所示:
// Corrected according to pseudocode!
if (arr[i]<=pivotValue) {
swap(arr, storeIndex, i);
storeIndex = storeIndex ++;
}或者你也可以这样做:
// Nice and clear!
if (arr[i]<=pivotValue) {
swap(arr, storeIndex++, i);
}从最新更新中得到的经验教训是:
if语句上,即使它们不是严格意义上必需的
最后一件事
当我提到数组的.length时,我的意思不是这样:
int[] A = {2, 5, 7, 3, 9, 0, 1, 6, 8};
QuickSort(A, 0, 8); // BAD! Don't hardcode array length!您应该这样做:
int[] arr = {2, 5, 7, 3, 9, 0, 1, 6, 8};
QuickSort(arr, 0, arr.length - 1); // GOOD!发布于 2010-06-08 15:53:11
随机生成器产生正值和负值。这就是为什么你最终会用一个负的q值调用交换。
发布于 2010-06-08 15:54:02
swap(A, p, rand.nextInt() % (q+1));生成的随机整数可能低于p,显然您希望它介于p和q之间。
https://stackoverflow.com/questions/2995535
复制相似问题