In front of the blog has written the English version of the Quick Find implementation examples, in the review, with a new understanding.
Thought
The key to the fast sorting algorithm is to first select a number in the array, then divide the number in the array into two parts, move to the left of the array smaller than the selected number, and move to the right of the array with a larger number than the selection.
"Example description"
Fast sort array data[] = {2,4,9,3,6,7,1,5}, set the first number as the base value, compare the other elements to it, the array length is 8, set two pointers i,j respectively point to the first subscript of the array to be sorted, to be compared to move:
First set of sorts:
Base value basis = data[0] = 2 The array is divided into the order of adjustment, so that less than 2 is on its left, greater than 2 on its right, the concrete realization of the idea, first to Data[j] operation, when data[j]> basis, J minus 1, moving forward one, Otherwise the value of data[j] will be filled data[i],data[j] will become the next object to be filled, I at this time move backward one lattice, and start the operation of Data[i], if the data[i]< basis,i plus 1, move backward one, otherwise it will data[i] The value fills at this time the data[j],data[i] becomes the object to be filled, J moves forward, repeating the previous steps, until I = j, stating that all is relatively finished, filling the position of this time data[i], so that can be divided by the baseline value of the left is less than basis, The right is greater than the basis case. The process is as follows:
This group is sorted with basis = Data[i],i = 0,j = 7, sorted after the resulting array {1,2,9,3,6,7,4,5}
Second set of sorts: i = 0, j = 0, basis = data[i] = 1, sorted after the resulting array {1,2,9,3,6,7,4,5}
Third group sort: i = 2, j=7, basis = data[i] = 9, sorted after the resulting array {1,2,5,3,6,7,4,9}
Fourth group sort: i=2,j=6,basis = data [2] = 5, sorted after the resulting array {1,2,4,3,5,7,6,9}
Fifth group sort: i = 2,j = 3,,basis = data[2] = 4, sorted after the resulting array {1,2,3,4,5,7,6,9}
Fifth group sort: i = 5,j = 6,basis = data[5] = 7, sorted after the resulting array {1,2,3,4,5,6,7,9}
"Test Code"
#include <stdio.h>voidquicksort (int Data[],int lo,int hi]{if(Lo>=hi) return; int I=lo, J=hi; int basis = data[lo];while (I < J) {while ( Data[j] > Basis) &&(i<J))J-- ; if(I<J)//This piece must have this judgment, otherwise it will be wrong { Data[i++] = data[j];} while ( Data[i] < basis) &&(i<J))i++;if(I<J) { Data[J--] = data[i]; } } Data[i] = basis;Quicksort data, lo, i-1);Quicksort Data, i+1, HI);}intMain () {int i; Int data[] = {2,4,9,3,6,7,1,5};Quicksort Data , 0, 7);for (i=0;i<8; i++) printf ("%d", Data[i]);Return0;}
Output
Fast sorting algorithm