The quick sorting algorithm is similar to the Merge Sorting Algorithm, which is the idea of splitting. The difference is that the Merge Sorting Algorithm differentiates two intervals by location, while the quick sorting algorithm uses values to differentiate two intervals. Therefore, merge sorting algorithms still need to be merged, while quick sorting does not.
The core of the quick sorting algorithm is to select a value 'distinct' in the interval so that all the values greater than 'distinct' are on its side, while those smaller than 'distinct' are on the other side.
Private Static int partititon (INT [] A, int L, int R) {int idx = mId3 (A, L, (L + r)/2, R ); int cursor = A [idx]; // the position of a [idx] is empty. Int I = L, j = r; while (I <j) {While (I <idx & A [I] <= random) I ++; if (I <j) {A [idx] = A [I]; idx = I ;}while (j> idx & A [J] >=pivot) j --; if (I <j) {A [idx] = A [J]; idx = J ;}} A [idx] = response; return idx ;}
It is skillful to select region in the interval. Generally, the number in the middle of the left, right, and right is used.
private static int mid3(int[] a,int l,int m,int r){ return a[l]<a[m]? (a[m]<a[r]? m : a[l]<a[r]? r:l ): ( a[m]>a[r]? m : a[l]>a[r]?r:l); }
This process is a bit like a trap. First, select "Rotate" and leave its value blank. Its position is marked as idx. Next, follow these steps:
Step 1: Fill in the first number on the left of idx greater than the limit value to the idx position, and then get a new pitfall, which is marked as idx.
Step 2: Fill in the first Vertex on the Right of idx that is smaller than the vertex in the idx position, and then get a new pitfall, which is marked as idx.
Repeat the preceding two steps until I> J.
Then the quick sorting algorithm is obtained. The Code is as follows:
public static void quickSort(int[] a,int l,int r){ if(l<r){ int paridx = partititon(a,l,r); quickSort(a,l,paridx-1); quickSort(a,paridx+1,r); } }
Very simple. The whole process is not obviously sorted, but it is always sorted. Algorithms are easy to think about. Learn and cherish it!
This article from the "every day progress a little" blog, please be sure to keep this source http://sbp810050504.blog.51cto.com/2799422/1556393
Quick sorting of the Application of divide and conquer ideas