Low ++;
R [High] = R [low];
}
R [low] = T;
Return low;
}
Int sort (int * r, int low, int high)
{
If (low> = high)
Return 0;
Int optional tloc = 0;
Required tloc = partition (R, low, high );
Sort (R, low, pivotloc-1 );
Sort (R, repeated tloc + 1, high );
}
Void quick_sort (int * r, int N) // calls the sort process.
{
Sort (R, 0, n-1 );
}
Let's talk about other related applications of quick sorting:
1. Median: In the fast sorting, the number smaller than R [low] is placed on the left, and the number greater than R [low] is placed on the right. The return line is the dividing line.Pivotloc,Median is to find(N & 0x01 )? R [Mid] :( R [Mid] + R [Mid + 1])/2, And the number in the middle, mid = n/2. Each timePartition (int * r, int low, int high)Then the location of the demarcation line is obtained. If the demarcation line is smaller than mid, the median is on the right; otherwise, the median is on the left. If you have followed my blog, you will remember my article"Find the minimum 0.1 million number from 1000"The heap is used, and it is also true to find the largest number. I will not implement it in heap mode. Here I will use quick sorting to solve this problem. The Code is as follows:
Int median (int * r, int N)
{
Int left = 0;
Int right = n-1;
Int mid = (left + right)/2;
Int num;
While (1)
{
Num = partition (R, left, right );
If (num = mid)
Break;
If (Num <mid) // The description is on the right.
Left = num + 1;
If (Num> mid)
Right = num-1;
}
Return (N & 0x01 )? R [Mid] :( R [Mid] + R [Mid + 1])/2;
}
In this way, the problem is solved ........
2. Find the number I from the number of N: That's the same. You just need to change it a little bit, so I will not post the code.
There is also the idea that "Quick Sort" is to use sub-governance, and I have time to sort out sub-governance and recursion. Now I want to learn too many things. The most important thing I want to learn is C ++. Although I have learned it before, it is really rare to actually use it, especially generic programming .. There are too many ways to use C and implement C ++. There is also a happy National Day...