1. /**
    2. * https://wiki.jikexueyuan.com/project/easy-learn-algorithm/fast-sort.html
    3. * 快速排序
    4. * @param arr
    5. * @param i
    6. * @param j
    7. */
    8. public static void quickSort(int[] arr, int i, int j){
    9. if (i>j) {
    10. return;
    11. }
    12. int pivot = arr[i];
    13. int left=i;
    14. int right=j;
    15. while (left!=right) {
    16. while ((right>left)&&arr[right] >= pivot) {
    17. right--;
    18. }
    19. //此时a[right]小于pivot
    20. while ((left < right)&&arr[left] <= pivot) {
    21. left++;
    22. }
    23. //此时a[left]大于pivot
    24. //交换a[i] a[j]
    25. if (left < right) {
    26. swap(arr, left, right);
    27. }
    28. }
    29. arr[i] = arr[left];
    30. arr[left]=pivot;
    31. quickSort(arr,i,left-1);
    32. quickSort(arr,left+1,j);
    33. }
    34. public static void quickSort(int[] arr) {
    35. quickSort(arr,0,arr.length-1);
    36. }
    37. private static void swap(int[] arr, int idx1,int idx2) {
    38. int tmp = arr[idx2];
    39. arr[idx2] = arr[idx1];
    40. arr[idx1] = tmp;
    41. }