【问题标题】:Java Quicksort Partition methodJava 快速排序分区方法
【发布时间】:2018-10-24 20:59:49
【问题描述】:

我正在编写一个 Java 快速排序方法。我目前的代码如下

    public class Quicksort {

   public static void main(String[ ] args)
   {
      final String BLANKS = "  "; // A String of two blanks
      int i;                      // Array index

      int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 };

      // Print the array before sorting:
      System.out.println("Here is the entire original array:");
      for (i = 0; i < data.length; i++)
         System.out.print(data[i] + BLANKS);
      System.out.println( );

      // Sort the numbers, and print the result with two blanks after each number.
      quicksort(data, 1, data.length-2);
      System.out.println("I have sorted all but the first and last numbers.");
      System.out.println("The numbers are now:");
      for (i = 0; i < data.length; i++)
         System.out.print(data[i] + BLANKS);
      System.out.println( );
   }

快速排序方法

    public static void quicksort(int[ ] data, int first, int n)
   {
      int pivotIndex; // Array index for the pivot element
      int n1;         // Number of elements before the pivot element
      int n2;         // Number of elements after the pivot element

      if (n > 1)
      {
         // Partition the array, and set the pivot index.
         pivotIndex = partition(data, first, n);

         // Compute the sizes of the two pieces.
         n1 = pivotIndex - first;
         n2 = n - n1 - 1;

         // Recursive calls will now sort the two pieces.
         quicksort(data, first, n1);
         quicksort(data, pivotIndex + 1, n2);
      }
   }

分区方法

   private static int partition(int[ ] data, int first, int n){ 
      int low = first;
      int high = n;
      int pivot = data[low];

      while(low < high){
         low ++;

         while(low <= high && data[low] < pivot){
            low ++;
         }
         while(high >= low && data[high] > pivot){
            high--;
         }
         if(low<=n && low < high){
            int temp = data[low];
            data[low] = data[high];
            data[high] = temp;
         }
      }
      return low;  
   }//end partition

}//end class

当我当前运行程序时,我得到的结果是 1000 80 0 10 70 60 90 20 30 40 50 -1000 在对分区方法进行了几次不同的尝试和重写之后,我仍然无法让数组正确排序。任务是对除第一个和最后一个数字之外的整个数组进行排序。

【问题讨论】:

  • 当我运行这个时,我得到了一个StackOverflowError
  • 获取任何可靠的分区实现。你的错误很多。省略第一个元素,使用 n=subarray size 作为右索引,依此类推。

标签: java algorithm sorting quicksort


【解决方案1】:

快速排序方法

public static void quicksort(int[ ] data, int first, int last){
          if (last-first > 1){
             // Partition the array, and set the pivot index.
             pivotIndex = partition(data, first, n);
             //n1 = pivotIndex - first; //problem is here 
             //  n2 = n - n1 - 1;       // and here 
             // Recursive calls will now sort the two pieces.
             quicksort(data, first, pivotIndex);
             quicksort(data, pivotIndex + 1, last);
          }
       }

分区方法实际的 Hoare 分区。

  private static int partition(int[ ] data, int first, int last){ 
  int low = first-1;
  int high = n+1;
  int pivot = data[low];

    while (true) {

        do {
            low++;
        }
        while (data[low] < pivot);

        do {
            high--;
        }
        while (data[j] > pivot);

        if (low < high) {
            int temp = data[low];
            data[low] = data[high];
            data[high] = temp;
        } else {
            return high;
        }
    }

} 

我已经更新了这两个函数现在你只需要调用。

int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 };
quicksort(data, 1, data.length-2);

Here is a good explanation of Hoare partition.

【讨论】:

  • 更好的答案是解释他的错误,而不是复制粘贴现成的众所周知的代码 sn-p
  • @Ashish 他正在尝试实现 hoare 的分区你已经实现了 lomuto 分区
  • @GuhanNagarajan 啊我的错谢谢,我不知道Hoare的分区方法:)
  • 感谢 cmets 我已更新答案。问题仅出在快速排序方法中,一旦我发现问题,将测试分区方法并更新我的答案。在那之前,我已经从链接中的答案中给出了正确的分区方法。
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2021-06-11
  • 2017-08-15
相关资源
最近更新 更多