@Test
public void test(){
int[] num = { 1,99, 3, 4, 8, 5, 10, 22, 15, 16,165,165 };
this.quickSort(num, 0, num.length - 1);
System.out.println(Arrays.toString(num));
}
public static void quickSort(int[] a, int start, int end) {
// 该值定义了从哪个位置开始分割数组
int ref;
if (start < end) {
// 调用partition方法对数组进行排序
ref = partition(a, start, end);
// 对分割之后的两个数组继续进行排序
quickSort(a, start, ref - 1);
quickSort(a, ref + 1, end);
}
}
/**
* 选定参考值对给定数组进行一趟快速排序
* @param a 数组
* @param start (切分)每个数组的第一个的元素的位置
* @param end (切分)每个数组的最后一个的元素位置
* @return 下一次要切割数组的位置
*/
public static int partition(int[] a, int start, int end) {
int refvalue = a[start];
while (start < end) {
while (end > start && a[end] >= refvalue) {
end--;
}
a[start] = a[end];
a[end] = refvalue;
while (end > start && a[start] <= refvalue) {
start++;
}
a[end] = a[start];
a[start] = refvalue;
return end;
}
a[start] = refvalue;
return start;
}