Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting_Quicksort - Fatal编程技术网

Java 快速排序出错(添加了一些重复项)

Java 快速排序出错(添加了一些重复项),java,sorting,quicksort,Java,Sorting,Quicksort,我对排序算法还不熟悉,似乎不知道出了什么问题。对于int[]uArr={5,2,10,7,6,0,8,1,9}的未排序数组,我得到了0,1,2,5,5,6,8,10(org.array中的9个被替换为重复的5,7丢失)。如果我向数组中添加重复项,那么在对数组进行排序后会得到更多的重复项和一些丢失的数字 public static void main(String args[]){ int [] uArr = {5,2, 10, 7, 6, 0, 8, 1, 9}; qSo

我对排序算法还不熟悉,似乎不知道出了什么问题。对于int[]uArr={5,2,10,7,6,0,8,1,9}的未排序数组,我得到了0,1,2,5,5,6,8,10(org.array中的9个被替换为重复的5,7丢失)。如果我向数组中添加重复项,那么在对数组进行排序后会得到更多的重复项和一些丢失的数字

 public static void main(String args[]){
     int [] uArr = {5,2, 10, 7, 6, 0, 8, 1, 9};

     qSort(uArr, 0, 8);
     
 }

 public static void qSort(int [] A, int low, int high){
    
    if (low < high){
   int pivotL = partition(A, low, high);
   qSort(A, low, pivotL);
 qSort(A, pivotL+1, high);
    }       
}
public static int partition(int [] arr, int low, int high){
 int pivot = arr[low];
 int leftwall = low;
 for (int i = low + 1; i < high; i++){
if (arr[i] < pivot){
 int temp = arr[i];
 arr[i] = arr[leftwall];
arr[leftwall] = temp;
leftwall += 1;

}  
}

int temp2 = pivot;
pivot = arr[leftwall];
arr[leftwall] = temp2;
return leftwall;
}
}
publicstaticvoidmain(字符串参数[]){
int[]uArr={5,2,10,7,6,0,8,1,9};
qSort(uArr,0,8);
}
公共静态void qSort(int[]A,int低,int高){
如果(低<高){
int pivotL=分区(A、低、高);
qSort(A、低、枢轴);
qSort(A,枢轴+1,高);
}       
}
公共静态int分区(int[]arr,int低,int高){
int pivot=arr[低];
int leftwall=低;
对于(int i=低+1;i<高;i++){
if(arr[i]
所以,有一件事你应该认真做,那就是开始编写文档。尽管这是一个小程序,但在编写代码时,您似乎忘记了自己在做什么

例如,数组中有9个元素,将偏移量作为0到8传递给排序(包括0到8):

  qSort( uArr, 0, 8 );
但是在分区例程中,您只对小于
高值的元素进行排序:

 for( int i = low + 1; i < high; i++ ) {
得到了这个输出:

run:
[5, 2, 10, 7, 6, 0, 8, 1, 9] low=0 high=8
[2, 0, 1, 5, 6, 5, 8, 10, 9] low=0 high=3
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=0 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=0 high=0
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=1 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=1 high=1
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=2 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=3 high=3
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=4 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=4 high=5
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=4 high=4
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=5 high=5
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=6 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=6 high=6
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=7 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=7 high=7
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=8 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9]
BUILD SUCCESSFUL (total time: 0 seconds)
我确实不得不盯着输出看了一会儿,但接下来我注意到两件事。5几乎是立即复制的,5也是第一个元素,这意味着它将由
分区
代码选择作为轴心。因此,看起来您在将pivot与数组重新合并时遇到了问题

更新2:OK发现另一个问题。我是通过手工写出数组并遍历
I
leftwall
的每个值来找到这个分区的。这里的问题是:

当枢轴5遇到数组中的元素
0
时,数组的其余部分尚未排序。
10
7
6
等不小于枢轴且未被触摸。因此,当您进行交换时,您看到了以下内容:

[2, 5, 10, 7, 6, 0, 8, 1, 9]
为此:

[2, 5, 0, 7, 6, 5, 8, 1, 9]
这是因为
leftwall
是1(它已与
2
交换,但没有任何其他数字,因此它只增加了一次),而且还有重复和丢失的数字。我就到此为止,因为你有一些相当大的问题

在这种情况下,您需要做的是用
0
交换
10
,而不是轴。这至少需要一个额外的指针。快速排序算法需要找到数组中的最低值和最高值,并且在外部
for
循环中有两个循环。这里有一种奇怪的递归插入排序。您需要考虑更多的方法,但是需要另外两个循环,嵌套在第一个循环中

[2, 5, 0, 7, 6, 5, 8, 1, 9]