gpt4 book ai didi

java - 关于快速排序

转载 作者:行者123 更新时间:2023-12-04 06:51:55 24 4
gpt4 key购买 nike

我已经编写了这段代码,但它会在控制台中打印这些堆栈跟踪,请帮助我,谢谢! (同样“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)

我需要你的帮助我真的很困惑!!!

最佳答案

您的代码的一些问题是:

  • 不要新建 Random 例如一次使用。只需创建一次,然后将其存储在 static 中字段,然后使用它为您的应用程序生成许多随机数。
  • 为枢轴创建随机索引时,它必须位于 p 之间和 q包括的。
  • 使用 Random.nextInt(int n)重载以生成给定范围内的随机数
  • 也许使用 lohi而不是 pq , 和 int[] arr而不是 int A[]
  • 你可以用它的 .length 得到一个数组的长度成员(member)

  • 至于快速排序算法本身,它看起来不像我以前见过的任何东西。我建议研究标准的命令式实现并对其进行调整。

    也可以看看
  • Wikipedia/Quicksort


  • 更新

    不幸的是,您有点混淆了维基百科的伪代码。你想调整这个算法:
      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 = 5right = 7 ,那么你想要 pivotIndex成为 5 + x哪里 x0 之间的随机数和 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 body 是这样的:
        // 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);
    }

    这个最新更新的教训是:
  • 始终正确缩进代码
  • 一个好的IDE可以让这一切变得轻而易举,你真的没有借口
  • 养成戴牙套的习惯 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!

    关于java - 关于快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2995535/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com