After graduating from college, I used to write all the algorithms in the data structure book, but it was not enough ..
After I learned to use sort in STL, I never wrote too fast. In the future, I want to write these words slowly .. Step by step.
# Include <cstdio>
/* Fast sorting adopts the divide and conquer idea, with two pointers left and right,
* One scan from left to right and one scan from right to left;
* For the left pointer, if the value of the element referred to by the Left pointer is less than or equal to the reference value,
* The pointer is shifted to the right. if the pointer is greater than the reference value, it is exchanged with the reference value;
* Similarly, for a right pointer, if the value of the element referred to by the right pointer is greater than or equal to the reference value,
* Move the pointer to the left. if the pointer is smaller than the reference value, it is exchanged with the reference value. */
// Partition the sequence
Int Part (int array [], int left, int right)
{
Int flag = array [left]; // reference
While (right> left)
{
While (right> left & array [right]> = flag)
{
Right --;
}
Array [left] = array [right];
While (right> left & array [left] <= flag)
{
Left ++;
}
Array [right] = array [left];
}
Array [left] = flag;
Return left;
}
// Recursive sorting
Int quicksort (int array [], int low, int high)
{
Int flag;
If (low {
Flag = Part (array, low, high );
Quicksort (array, low, flag-1 );
Quicksort (array, flag + 1, high );
}
}
Author: sunrise