1. class Solution {
    2. public int[] quickSort(int[] arr) {
    3. if (arr == null || arr.length == 1) {
    4. return arr;
    5. }
    6. quick(arr, 0, arr.length - 1);
    7. return arr;
    8. }
    9. private void quick(int[] arr, int i, int j) {
    10. if (i >= j) {
    11. return;
    12. }
    13. int pos = getPos(arr, i, j);
    14. quick(arr, i, pos - 1);
    15. quick(arr, pos + 1, j);
    16. }
    17. private int getPos(int[] arr, int i, int j) {
    18. int pos = i;
    19. int val = arr[j];
    20. for (int k = i; k < j; k++) {
    21. if (arr[k] < arr[j]) {
    22. if (k == pos) {
    23. pos++;
    24. } else {
    25. int temp = arr[pos];
    26. arr[pos++] = arr[k];
    27. arr[k] = temp;
    28. }
    29. }
    30. }
    31. int temp = arr[pos];
    32. arr[pos] = val;
    33. arr[j] = temp;
    34. return pos;
    35. }
    36. }