private static void quickSort(int left, int right, int[] array) {
if (left > right) {
return;
}
int leftIndex = left, rightIndex = right, flag = array[left];
while (leftIndex != rightIndex) {
// 从右开始找比指定的基准数小的数
while (leftIndex < rightIndex && array[rightIndex] >= flag) {
rightIndex--;
}
// 从左开始找比指定的基准数大的数
while (leftIndex < rightIndex && array[leftIndex] <= flag) {
leftIndex++;
}
// 交换
if (leftIndex < rightIndex) {
int tmp = array[leftIndex];
array[leftIndex] = array[rightIndex];
array[rightIndex] = tmp;
}
}
// 重置被用来比较的flag数
array[left] = array[leftIndex];
array[leftIndex] = flag;
// 递归分治
quickSort(left, leftIndex - 1, array);
quickSort(rightIndex + 1, right, array);
}