【问题标题】:about Quick Sort关于快速排序
【发布时间】:2010-06-08 07:46:28
【问题描述】:

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

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

【问题讨论】:

  • 请同时发布堆栈跟踪。

标签: java data-structures sorting quicksort


【解决方案1】:

您的代码存在一些问题:

  • 不要为一次性使用创建新的Random 实例。只需创建一次,将其存储在 static 字段中,然后使用它为您的应用程序生成许多随机数。
  • 为枢轴创建随机索引时,它必须位于pq 之间。
  • 使用Random.nextInt(int n)重载生成给定范围内的随机数
  • 也许使用lohi代替pq,以及int[] arr代替int A[]
  • 您可以通过.length 成员获取数组的长度

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

另见


更新

不幸的是,您在某种程度上混淆了来自维基百科的伪代码。你想调整这个算法:

  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)

请注意,上述算法选择了leftright 之间的子数组的中间元素作为枢轴索引。由于你想要一个随机快速排序,你想要在leftright之间选择一个随机索引,所以公式需要更改如下:

  pivotIndex := left + (random number between 0 and right-left inclusive)

因此,例如,如果left = 5right = 7,那么您希望pivotIndex 成为5 + x,其中x 是介于07-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);
    }

这次最新更新的教训是:

  • 始终正确缩进代码
    • 一个好的 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!

【讨论】:

  • 我有一个问题 nextInt() 不是静态方法!!!我怎么能像你上面写的那样称呼它??
  • @matin1234:很抱歉,这有点令人困惑。 Random.nextInt(int n),我的意思是 nextInt(int n)Random 实例上,所以它类似于你对 nextInt() 所做的事情,但只需给它额外的参数,而不是之后再做 %
  • @polygenelubricants:它(应该是)随机快速排序。唯一的区别是它使用随机元素作为枢轴。这显着改变了遇到最坏情况的频率,并且是改进快速排序的最佳策略之一(特别是在处理可能由恶意来源制造的不安全输入以引发最坏情况时– 用于例如 DDOS 攻击)。
  • 非常感谢您的帮助,我已经完成了您所写的(我已经编辑了我的帖子),但它没有对我的数组进行排序!!!
  • +1 表示非常出色的工作具有指导意义,而不仅仅是提供解决方案,多基因润滑剂
【解决方案2】:

随机生成器产生正值和负值。这就是为什么最终你用一个负的q 值调用 swap。

【讨论】:

    【解决方案3】:
    swap(A, p, rand.nextInt() % (q+1));
    

    生成的随机整数可能低于 p,显然您希望它介于 p 和 q 之间。

    【讨论】:

      【解决方案4】:

      因为这是不是家庭作业,我会采用家庭作业类别来包含自学,你应该只使用标准Arrays.sort(int[]).

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        • 2019-06-01
        • 2016-12-23
        • 1970-01-01
        • 2015-05-26
        • 2013-12-08
        相关资源
        最近更新 更多