使用hoarepartition的QuickSort产生不正确的输出
我的作业要求我实现与教科书中的伪代码完全相同的QuickSort算法: 它指定使用hoarepartition进行分区。
pivot <- A[leftMost]
i <- leftMost; j <- rightMost+1
这是我的代码:
import java.util.Arrays;
public class Main{
public static void main(String args[]){
int[] array1 = {10, 4, 2, 8, 7, 3, 5, 9, 6, 1};
int n1 = array1.length;
System.out.print("Before sorting is: ");
System.out.println(Arrays.toString(array1));
System.out.printf("After sorting is: ");
Quicksort(array1, 0, n1 -1);
System.out.println(Arrays.toString(array1));
} //end main
public static void Quicksort(int[]array, int start, int end){
if(start<end){
int pivot = HoarePartition(array, start, end);
Quicksort(array, start, pivot-1);
Quicksort(array, pivot+1, end);
}
} //end Quicksort()
public static int HoarePartition(int[] array, int start, int end){
int pivot = array[start];
int i = start;
int j = end +1;
while(true){
while(array[i] < pivot)
i++;
do{j--}while(array[j] > pivot)
if(i <= j)
return j;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
} //end while
} //end HoarePartition()
输出是:
Before sorting is: [10, 4, 2, 8, 7, 3, 5, 9, 6, 1]
After sorting is: [1, 3, 2, 5, 7, 4, 8, 9, 6, 10]
显然,它没有正确排序,我一遍又一遍地考虑,但仍然不知道我的算法的哪一部分是错误的...
My homework asked me to implement Quicksort algorithm exactly the same as the pseudocode in the textbook:
It specifies to use HoarePartition to do the partition.
pivot <- A[leftMost]
i <- leftMost; j <- rightMost+1
and here's my code:
import java.util.Arrays;
public class Main{
public static void main(String args[]){
int[] array1 = {10, 4, 2, 8, 7, 3, 5, 9, 6, 1};
int n1 = array1.length;
System.out.print("Before sorting is: ");
System.out.println(Arrays.toString(array1));
System.out.printf("After sorting is: ");
Quicksort(array1, 0, n1 -1);
System.out.println(Arrays.toString(array1));
} //end main
public static void Quicksort(int[]array, int start, int end){
if(start<end){
int pivot = HoarePartition(array, start, end);
Quicksort(array, start, pivot-1);
Quicksort(array, pivot+1, end);
}
} //end Quicksort()
public static int HoarePartition(int[] array, int start, int end){
int pivot = array[start];
int i = start;
int j = end +1;
while(true){
while(array[i] < pivot)
i++;
do{j--}while(array[j] > pivot)
if(i <= j)
return j;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
} //end while
} //end HoarePartition()
And the output is:
Before sorting is: [10, 4, 2, 8, 7, 3, 5, 9, 6, 1]
After sorting is: [1, 3, 2, 5, 7, 4, 8, 9, 6, 10]
Apparently, it's not sorted correctly, I've been thinking over and over again but still don't know which part of my algorithm is wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个递归电话应该是:
经典的Hoare分区
The two recursive calls should be:
Classic Hoare partition
您编写的代码有一些问题。
在您的第一个递归调用
QuickSort
中,应包括枢轴绑定而不是传递pivot-1
。在您的
中
返回条件不仅应检查两个索引
i
和j
已经满足,而且是否相互交叉,如果(I&gt; =(i&gt; =) j)返回j;最内向的循环应写为两个
,而
循环而不是,而
和 do- do-while < /p>输出
There are a few problems with the code you wrote.
In your first recursive call of
quicksort
, the pivot bound should be included instead of passingpivot-1
.In your
HoarePartition
method you forgot to move the indexesi
andj
after the swap.The returning condition should check not only whether the two indexes
i
andj
have met but also if they have crossed each otherif (i >= j) return j;
The innermost loops should be written as two
while
loops instead of awhile
and ado-while
Output
谢谢!我发现了我犯的错误:
我在
hoarepartition()
中设置了最初的值,在调整i,j和键入的循环后,它可以正常工作!Thanks! I found the mistake I did:
I set the initial value wrong in
HoarePartition()
, after adjust the i, j and the loop I typed, it works correctly!