The idea of fast sequencing is to divide and conquer, using recursion to achieve the effect of fast sorting
First of all to select a base number, the general selection of the leftmost number is the base number, the goal of the order is to make the base number of the left all less than the base number, the right is more than the base number. Then this sort of function is called again on the left and right side with this datum number as the dividing line, until it is all in order. Brief description of the process:
With 8 9 4 7 2 6 First Choice
1. Select two Sentinel i,j to point to 8, 6, Base number 8
2. Starting from the J Sentinel, because J points to 6 is less than the base number 8, does not conform to the number of J point is greater than the requirement of 8, so the number of J points is covered by the number I point, while I point to the number of 9
6 9 4 7 2 6
3. At this point I point 9 is greater than the base number 8, does not conform to the base number of the left is less than the base number, the right is greater than the base number, so I point to the number of the number of J points, and J--, J Point 7
6 9 4 7 2 9
Repeat the above steps until Sentinel I and Sentinel J meet
4. The final step is to put the benchmark value in the middle
Code implementation:
void QuickSort (int a[], int low, int. high)
{
int i = low, j = high;//each time the i,j points to the lowest element, and the highest one element
int temp = a[low];//each time the leftmost number is selected as the base number
while (I < J)//The condition of the end of each cycle is i = = J
{
while (I < J && A[j] >= temp) j--;//first from the left to find the number less than the base number
if (I < j) {
A[i] = A[j];
i++;
}//and I-pointing number Exchange
while (I < J && A[i] <= temp) i++;//Find the number that is greater than the base number to the left of the base number
if (I < j) {
A[J] = A[i];
j--;
}//to the base number to the right.
}//the base number in the middle after the execution of the above
A[i] = temp;
if (Low < i)//if it is the left side of the base number
QuickSort (A, low, i-1);//will be the highest bit i-1
if (I < high)//if it is the right side of the base number
QuickSort (A, j+1, high);//minimum bit is reference bit +1
}
Summary of Quick Sort