Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java快速排序分区方法_Java_Algorithm_Sorting_Quicksort - Fatal编程技术网

Java快速排序分区方法

Java快速排序分区方法,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,我正在编写一个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,

我正在编写一个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( );
   }
分割法

   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
私有静态int分区(int[]数据,int-first,int-n){
int低=第一;
int高=n;
int pivot=数据[低];
while(低<高){
低++;
while(低=低&数据[高]>透视){
高--;
}
如果(低快速排序法

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);
          }
       }
划分方法实际霍尔的划分

  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;
        }
    }

} 

当我运行此程序时,我得到了一个
StackOverflowerError
获取任何可靠的分区实现。您的分区包含很多错误。省略第一个元素,使用n=子数组大小作为正确的索引等等。更好的答案应该是解释他的错误,而不是复制一个现成的已知代码snippet@Ashish他试图暗示门特霍尔的分区你已经实现了洛穆托partition@GuhanNagarajan啊,糟糕的谢谢,我不知道霍尔的分区方法:)感谢您的评论,我已经更新了答案。问题只存在于快速排序方法中,我将测试分区方法,并在其中找到问题后更新我的答案。在此之前,我已从链接中的答案中给出正确的分区方法。
  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);