Click “programmer small Grey” above, choose to pay attention to the public account

Interesting and meaningful articles are delivered at the first time!

 

————— the next day —————

public class QuickSort {

  1. public static void quickSort(int[] arr, int startIndex, int endIndex) {

  2. // End recursion condition: startIndex is equal to endIndex

  3.     if (startIndex >= endIndex) {

  4.         return;

  5.     }

  6. // Get the base element position

  7.     int pivotIndex = partition(arr, startIndex, endIndex);

  8. // Divide and conquer two parts of a recursive sequence

  9.     quickSort(arr, startIndex, pivotIndex – 1);

  10.     quickSort(arr, pivotIndex + 1, endIndex);

  11. }


  12.  

  13. private static int partition(int[] arr, int startIndex, int endIndex) {

  14. // Take the element in the first position as the base element

  15.     int pivot = arr[startIndex];

  16.     int left = startIndex;

  17.     int right = endIndex;

  18. // The position of the pit, initially equal to the position of pivot

  19.     int index = startIndex;

  20.  

  21. // The major loop ends when the left and right Pointers overlap or interlace

  22.     while ( right >= left  ){

  23. // The right pointer is compared from right to left

  24.         while ( right >= left ) {

  25.             if (arr[right] < pivot) {

  26.                 arr[left] = arr[right];

  27.                 index = right;

  28.                 left++;

  29.                 break;

  30.             }

  31.             right–;

  32.         }

  33. // The left pointer is compared from left to right

  34.         while ( right >= left ) {

  35.             if (arr[left] > pivot) {

  36.                 arr[right] = arr[left];

  37.                 index = left;

  38.                 right–;

  39.                 break;

  40.             }

  41.             left++;

  42.         }

  43.     }

  44.     arr[index] = pivot;

  45.     return index;

  46. }


  47.  

  48. public static void main(String[] args) {

  49. Int [] arr = new int[] {4,7,6,5,3,2,8,1};

  50.     quickSort(arr, 0, arr.length-1);

  51.     System.out.println(Arrays.toString(arr));

  52. }

}

public class QuickSort {

  1. public static void quickSort(int[] arr, int startIndex, int endIndex) {

  2. // End recursion condition: startIndex is equal to endIndex

  3.     if (startIndex >= endIndex) {

  4.         return;

  5.     }

  6. // Get the base element position

  7.     int pivotIndex = partition(arr, startIndex, endIndex);

  8. // Sort by base element recursively

  9.     quickSort(arr, startIndex, pivotIndex – 1);

  10.     quickSort(arr, pivotIndex + 1, endIndex);

  11. }


  12.  

  13. private static int partition(int[] arr, int startIndex, int endIndex) {

  14. // Take the element in the first position as the base element

  15.     int pivot = arr[startIndex];

  16.     int left = startIndex;

  17.     int right = endIndex;

  18.  

  19. while( left ! = right) {

  20. // Control the right pointer to compare and move to the left

  21.         while(left<right && arr[right] > pivot){

  22.             right–;

  23.         }

  24. // Control the right pointer to compare and move right

  25.         while( left<right && arr[left] <= pivot) {

  26.             left++;

  27.         }

  28. // Swap left and right elements

  29.         if(left<right) {

  30.             int p = arr[left];

  31.             arr[left] = arr[right];

  32.             arr[right] = p;

  33.         }

  34.     }

  35.  

  36. // Pivot and pointer overlap point switch

  37.     int p = arr[left];

  38.     arr[left] = arr[startIndex];

  39.     arr[startIndex] = p;

  40.  

  41.     return left;

  42. }


  43.  

  44. public static void main(String[] args) {

  45. Int [] arr = new int[] {4,7,6,5,3,2,8,1};

  46.     quickSort(arr, 0, arr.length-1);

  47.     System.out.println(Arrays.toString(arr));

  48. }

}

 

Compared with the digging method, the pointer swap method performs fewer element swaps in the partition method.

Let’s look at the code below:

 

public class QuickSortWithStack {

  1. public static void quickSort(int[] arr, int startIndex, int endIndex) {

  2. // Use a collection stack instead of a recursive function stack

  3.     Stack<Map<String, Integer>> quickSortStack = new Stack<Map<String, Integer>>();

  4. // Start and end indices of the entire sequence are hashed into the stack

  5.     Map rootParam = new HashMap();

  6.     rootParam.put(“startIndex”, startIndex);

  7.     rootParam.put(“endIndex”, endIndex);

  8.     quickSortStack.push(rootParam);

  9.  

  10. // The loop ends when the stack is empty

  11. while (! quickSortStack.isEmpty()) {

  12. // The top element goes off the stack to get the start and end indices

  13.         Map<String, Integer> param = quickSortStack.pop();

  14. // Get the base element position

  15.         int pivotIndex = partition(arr, param.get(“startIndex”), param.get(“endIndex”));

  16. // Divide the base element into two parts and push the start and end indices of each part onto the stack

  17.         if(param.get(“startIndex”) <  pivotIndex -1){

  18.             Map<String, Integer> leftParam = new HashMap<String, Integer>();

  19.             leftParam.put(“startIndex”,  param.get(“startIndex”));

  20.             leftParam.put(“endIndex”, pivotIndex -1);

  21.             quickSortStack.push(leftParam);

  22.         }

  23.         if(pivotIndex + 1 < param.get(“endIndex”)){

  24.             Map<String, Integer> rightParam = new HashMap<String, Integer>();

  25.             rightParam.put(“startIndex”, pivotIndex + 1);

  26.             rightParam.put(“endIndex”, param.get(“endIndex”));

  27.             quickSortStack.push(rightParam);

  28.         }

  29.     }

  30. }

  31.  

  32. private static int partition(int[] arr, int startIndex, int endIndex) {

  33. // Take the element in the first position as the base element

  34.     int pivot = arr[startIndex];

  35.     int left = startIndex;

  36.     int right = endIndex;

  37.  

  38. while( left ! = right) {

  39. // Control the right pointer to compare and move to the left

  40.         while(left<right && arr[right] > pivot){

  41.             right–;

  42.         }

  43. // Control the right pointer to compare and move right

  44.         while( left<right && arr[left] <= pivot) {

  45.             left++;

  46.         }

  47. // Swap left and right elements

  48.         if(left<right) {

  49.             int p = arr[left];

  50.             arr[left] = arr[right];

  51.             arr[right] = p;

  52.         }

  53.     }

  54.  

  55. // Pivot and pointer overlap point switch

  56.     int p = arr[left];

  57.     arr[left] = arr[startIndex];

  58.     arr[startIndex] = p;

  59.  

  60.     return left;

  61. }

  62.  

  63. public static void main(String[] args) {

  64. Int [] arr = new int[] {4,7,6,5,3,2,8,1};

  65.     quickSort(arr, 0, arr.length-1);

  66.     System.out.println(Arrays.toString(arr));

  67. }

}

 

Compared to the recursive implementation, the code changes are only in the quickSort method. In this method, a stack of Map type elements is introduced to store the starting and ending subscripts for each swap.


Each time through the loop, the top element is removed from the stack, sorted, and divided into left and right parts according to the position of the base element, which are pushed into the stack separately. When the stack is empty, the sorting is complete and the loop exits.