Fast sorting is widely used as an efficient sorting algorithm, and the Arrays.sort method used in Sun's JDK is the fast platoon.
The
has adopted the classic idea of divide and conquer (Divide and conquer):
Divide: Select a primitive x (typically the first element of the array), dividing the array into two parts by some partition operation (partitioning): The left half is less than or equal to X, The right half is greater than or equal to X.
Conquer: the left and right two sub arrays recursively call the divide process.
Combine: Quick Row as an in-place sort algorithm, no merge operations are required
can see that the core of the fast row is the partitioning process (partitioning), with an example to explain in detail how to partition an array (the graph 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 begins with 1)
loop invariants: Elements between 2~i are less than or equal to p,i+1~j elements are greater than or equal to P
Cycle Process: J from 2 to N, to investigate the elements of J position, if greater than or equal to p, continue to cycle. If it is less than p, the elements of the J position (which should not appear in the I+1~j interval) and the i+1 position (which is still in the I+1~j interval after the exchange) are exchanged and will be i+1. This keeps the loop invariant (see the circular invariants above). Until J=n, complete the last loop operation.
Note that after the loop is complete, you also need to exchange the elements of the I position and the first element of the array to meet the requirements we set first (corresponding to step I in the graph).
Attentive readers may think of another, more straightforward partitioning method, in which an element that is smaller than a primitive is stored in the left half of the array, and an element larger than the primitive is stored in the right half of the array, when the primitive is taken out of another array of the same size. The complexity of such operations is also linear, i.e. Theta (n). But the complexity of the space increased by one fold. This is also the advantage of fast-ranked in-place sorting.
Copy Code code as follows:
public class QuickSort {
private static void QuickSort (int[] array,int start,int end)
{
if (start<end)
{
int key=array[start];//Initialize Save primitives
int i=start,j;//initialization i,j
for (j=start+1;j<=end;j++)
if (Array[j]<key)//If the element here is less than the primitive, the element is exchanged with the i+1 element, and I plus 1, such as greater or equal to the primitive, continues the loop
{
int temp=array[j];
array[j]=array[i+1];
array[i+1]=temp;
i++;
}
}
array[start]=array[i];//Exchange I elements and primitives
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,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);
}
}
}