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) and divide the arrayTwo parts:The left half is less than or equal to X, and the right half is greater than or equal to X.
Conquer:The left and right subarrays recursively call the Divide process.
Combine:As an in place sort algorithm, fast sorting does not require 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)
Cycle 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 ).
Readers may think of another more straightforward partitioning method, that is, removing the elements from an array of the same size, elements smaller than the elements are stored in the left half of the array, and 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.
Copy codeThe Code is as follows:
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]; // exchanges elements and elements in the I field.
Array [I] = key;
QuickSort (array, start, I-1); // recursive call
QuickSort (array, I + 1, end );
}
}
Public static void main (String [] args)
{
Int [] array = new int [] {11,213,134, 43 };
QuickSort (array, 0, array. length-1 );
For (int I = 0; I <array. length; I ++)
{
System. out. println (I + 1) + "th:" + array [I]);
}
}
}