Quick sorting is widely used as an efficient sorting algorithm. The arrays. sort method in Sun's JDK uses quick sorting.
Kubernetes adopts the classic divide and conquer idea ):
Divide: select a primitive X (usually the first element of the array), divide the array into two parts through a partition operation (partitioning): The left half is less than or equal to X, the right half is greater than or equal to X.
Conquer: recursively calls the divide process from the left and right child arrays.
Combine: Fast sorting is used as in place sort without any merge operations.
It can be seen that the core part of the fast sorting is the partitioning process (partitioning). The following uses an example to explain in detail how to divide arrays (the figure is taken from the introduction to algorithms).
Initialization:Select the primitive p = 2, which is the first element of the array. I = 1, j = I + 1 = 2 (array subscript starts with 1)
Loop Invariant:2 ~ All elements between I are less than or equal to P, I + 1 ~ Elements between J are greater than or equal to P
Loop process:From 2 to N, the element at the J position is investigated. If it is greater than or equal to P, the loop continues. If the value is less than P, the elements at the J position (should not appear in I + 1 ~ J) and I + 1 (after the switch, it is still in the I + 1 ~ In the J interval), and the I + 1 is used to maintain the cyclic invariant (see the preceding circular invariant description ). Until J = N, the last loop operation is completed.
Note that after completing the loop, you also need to swap the elements at the I position with the first element of the array to meet our first set requirements (corresponding to step I in the figure ).
Careful readers may think of another more straightforward partitioning method, that is, the elements are extracted from another array of the same size, and elements smaller than the elements are stored in the left half of the array, elements larger than the base element are stored in the right half of the array. The operation complexity is also linear, that is, theta (n ). However, the space complexity is doubled. This is also the advantage of fast sorting in place.
Finally, we attached the Java code implementation of quickshield:
Public class quicksort {Private Static void quicksort (INT [] array, int start, int end) {If (start <End) {int key = array [start]; // initialize and save the primitive int I = start, J; // initialize I, j For (j = start + 1; j <= end; j ++) {If (array [J] <key) // If the element is smaller than the primitive, the element is exchanged with the element at I + 1, and I is added to 1, if it is greater than or equal to a primitive, the loop continues {int temp = array [J]; array [J] = array [I + 1]; array [I + 1] = temp; I ++ ;}} array [start] = array [I]; // swap the elements at I and the base element array [I] = key; quicksort (array, start, I-1 ); // recursively call quicksort (array, I + 1, end) ;}} public static void main (string [] ARGs) {int [] array = new int [] {11,213,134, 44,77, 78,23, 43}; quicksort (array, 0, array. length-1); For (INT I = 0; I <array. length; I ++) {system. out. println (I + 1) + "TH:" + array [I]) ;}}
The running result is as follows: