如果在分区方法内我想初始化 l=start 或 l=start+1 和 end =end-1 ,我需要对分区循环进行哪些更改?如果这些值初始化为l=start-1; r=结束;循环工作正常,但不满足不变量。有什么想法吗?
public class qs {
private qs(){}
/**
* Sort an array in ascending order
* @param arr array to sor */
public static void quickSort(int[] arr){
qs(arr, 0, arr.length);
}
/**
* Sort a region of an array in ascending order.
* Elements outside the given region are unchanged.
* requires: 0 <= start <= end <= arr.length
* @param arr array to sort
* @param start start of region (inclusive)
* @param end end of region (exclusive)
*/
private static void qs(int[] arr, int start, int end){
if (end <= start+1){ //region of length 0 or 1
return;
}
int x = arr[start];
int p = partition(arr, start+1, end, x);
//now swap arr[start] with arr[p-1]
arr[start] = arr[p-1];
arr[p-1] = x;
qs(arr, start, p-1);
qs(arr, p, end);
}
/**
* Partition a region of an array.
* Rearranges elements in region so that small ones
* all have smaller indexes than the big ones.
* Elements outside the region are unchanged.
* requires: 0 <= start <= end <= arr.length
* @param arr array to partition
* @param start start of region (inclusive)
* @param end end of region (exclusive)
* @param x pivot - "small" and "big" are <x, >=x.
* @return start index (inclusive) of big elements
* in region after partition.
*/
private static int partition(
int[] arr, int start, int end, int x)
{
int l = start ;
int r = end-1;
while (l<r) {
while(++l< r && arr[l] < x);
while(--r > l && arr[r] > x);
if(l >= r) // if pointers cross,
break; // partition done
else {
int temp;
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
return l;
}
public static void main(String[] args) {
int[] a = {15,8,4,8,9,6,};
quickSort(a);
for (int i = 0; i < a.length; i++){
System.out.print(" "+a[i]);
}
}
}
最佳答案
枢轴应该是中间点,而不是数组的起点。
int x = arr[start];
将此行替换为...
int x = arr[(start+end)/2]
关于Java Quicksort.识别分区中的变化 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13663457/