A quick sort of in-depth detailed and Java implementation _java

Source: Internet
Author: User
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 fast-platoon adopts the classic idea of divide and conquer (Divide and conquer):

Divide:Select a primitive X (General selection of the first element of the array) and divide the array into a partition operation (partitioning) two sections: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 two sub arrays recursively invoke the divide procedure.
Combine:The quick row is the in-place sort algorithm and does not require any merge operations
It can be seen that the core of the fast row is the partitioning process (partitioning), with an example to explain in detail how to divide an array (the graph is taken from the introduction to algorithms).
Initialize: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 the elements between p,i+1~j are greater than or equal to P

Loop Process:J, from 2 to N, examines the elements of the J position, and if it is greater than or equal to P, the loop continues. 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 (the step I in the corresponding 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];//Initialization 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 than or equal to the primitive, continues to 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]);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.