Quick sorting:
The basic idea of this method is:
1. First, extract a number from the series as the reference number.
2. In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.
3. Repeat the second step for the left and right intervals until each interval has only one number.
Further mining of quick sorting involves digging holes and partitioning. This method is more practical and simple:
Summarize the number of pitfalls
1. I = L; j = R; dig out the reference number to form the first pit a [I].
2. j -- find a smaller number than it in the back and forward, and dig out the number in the previous pit a [I.
3. I ++ finds a larger number from the front to the back, and then mines the number to fill in the previous pit a [j.
4. Repeat steps 2 and 3 until I = j and enter the baseline number in a [I.
It is easy to get the code:
# Include
# Include
Void quick_sort (int s [], int l, int r );
Int main (void)
{
Int a [10] = {20, 55 };
Int I;
Quick_sort (a, 0, 9 );
For (I = 0; I <10; I ++)
Printf ("% d", a [I]);
Return 0;
}
// Quick sorting
Void quick_sort (int s [], int l, int r)
{
If (l <r)
{
// Swap (s [l], s [(l + r)/2]); // exchanges the number in the middle with the first number.
Int I = l, j = r, x = s [l];
While (I <j)
{
While (I <j & s [j]> = x) // find the first number less than x from the right to the left.
J --;
If (I <j)
S [I ++] = s [j];
While (I <j & s [I] <x) // from left to right find the first number greater than or equal to x
I ++;
If (I <j)
S [j --] = s [I];
}
S [I] = x;
Quick_sort (s, l, I-1); // recursive call
Quick_sort (s, I + 1, r );
}
}