[英]Java Quick Sort
I am implementing quicksort in Java with the following requirements: 我正在使用Java实现quicksort,具有以下要求:
Use recursion (in your quicksort implementation) down to a stopping case of a partition of size k or smaller. 使用递归(在快速排序实现中)直到大小为k或更小的分区的停止情况。 For these partitions, use an insertion sort to finish. 对于这些分区,请使用插入排序来完成。
I'm confused as to how to implement this or even what it is asking. 我很困惑如何实现这个甚至是它所要求的。 Here is what I have for the normal quicksort. 这是我对普通快速排序的看法。 How could I modify this to meet the requirements? 我怎么能修改它以满足要求?
public static void quick_srt(int array[],int low, int n){
int lo = low;
int hi = n;
if (lo >= n) {
return;
}
int mid = array[(lo + hi) / 2];
while (lo < hi) {
while (lo<hi && array[lo] < mid) {
lo++;
}
while (lo<hi && array[hi] > mid) {
hi--;
}
if (lo < hi) {
int T = array[lo];
array[lo] = array[hi];
array[hi] = T;
}
}
if (hi < lo) {
int T = hi;
hi = lo;
lo = T;
}
quick_srt(array, low, lo);
quick_srt(array, lo == low ? lo+1 : lo, n);
}
The partition size is hi - lo
. 分区大小是hi - lo
。 So you could add something like, 所以你可以添加类似的东西,
int k = 7;
// ...
if (hi - lo < k) {
insertion_srt(array, lo, hi);
return;
}
before int mid
. 在int mid
之前。 Implementing insertion_srt(int[], int, int)
left as an exercise for the reader. 实现insertion_srt(int[], int, int)
作为读者的练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.