/**
* https://wiki.jikexueyuan.com/project/easy-learn-algorithm/fast-sort.html
* 快速排序
* @param arr
* @param i
* @param j
*/
public static void quickSort(int[] arr, int i, int j){
if (i>j) {
return;
}
int pivot = arr[i];
int left=i;
int right=j;
while (left!=right) {
while ((right>left)&&arr[right] >= pivot) {
right--;
}
//此时a[right]小于pivot
while ((left < right)&&arr[left] <= pivot) {
left++;
}
//此时a[left]大于pivot
//交换a[i] a[j]
if (left < right) {
swap(arr, left, right);
}
}
arr[i] = arr[left];
arr[left]=pivot;
quickSort(arr,i,left-1);
quickSort(arr,left+1,j);
}
public static void quickSort(int[] arr) {
quickSort(arr,0,arr.length-1);
}
private static void swap(int[] arr, int idx1,int idx2) {
int tmp = arr[idx2];
arr[idx2] = arr[idx1];
arr[idx1] = tmp;
}