3-way partition quick sort
作者:
dsf2
,
2021-03-01 00:13:49
,
所有人可见
,
阅读 357
public static void quickSort(int[] arr, int l, int r) {
if (l >= r)
return;
int ran = arr[l + (int)(Math.random() * ((r - l) + 1))];
int begin = l;
int end = r;
int idx = l;
while (idx <= r) {
int val = arr[idx];
if (val == ran) {
idx++;
} else if (val < ran) {
int temp = arr[l];
arr[l++] = val;
arr[idx++] = temp;
} else {
int temp = arr[r];
arr[r--] = val;
arr[idx] = temp;
}
}
quickSort(arr, begin, l - 1);
quickSort(arr, r + 1, end);
return;
}