java快速排序代码实现
long start = System.nanoTime();
sort(array, 0, array.length - 1);
long end = System.nanoTime();
System.out.println(end - start);
// 优化后
private static void sort(int[] array, int low, int high) {
int i, j, index;
if (low > high) {
return;
}
i = low; // 哨兵i
j = high; // 哨兵j
index = array[i]; // 第一次排序的基准数
while (i < j) { // 从表的俩端往中间开始扫描
while (i < j && array[j] >= index)
j--;
if (i < j)
array[i++] = array[j];
while (i < j && array[i] < index)
i++;
if (i < j)
array[j--] = array[i];
}
array[i] = index;
sort(array, low, i - 1);
sort(array, i + 1, high);
}