Quick sorting(Quick
Sort) is an improvement in Bubble sorting. The basic idea is to split the records to be sorted into two separate parts by one sort. the keywords of some records are smaller than those of other records, then, the two records can be sorted separately to achieve the whole sequence order.
// Quick sorting
Void Cexchangesort: quicksort ( Void )
{
Const Int Count = 9 , Length = Count - 1 ;
Int L [count] = { 0 , 49 , 38 , 65 , 97 , 76 , 13 , 27 , 49 };
Int Low = 1 ;
Int High = Length;
Quicksort (L, low, high );
// Print the sorting result.
For ( Int I = 0 ; I <= Length; ++ I)
{
Cout < L [I] < " \ T " ;
}
Cout < Endl;
}
// Recursion of quick sorting
Void Cexchangesort: quicksort ( Int L [], Int Low, Int High)
{
If (Low < High)
{
Int Pivotloc = Partition (L, low, high ); // Position where the pivot record is obtained
Quicksort (L, low, pivotloc - 1 );
Quicksort (L, cmdtloc + 1 , High );
}
}
// Fast sorting of partitions
Int Cexchangesort: partition ( Int L [], Int Low, Int High)
{
L [ 0 ] = L [low];
Int Pivotkey = L [low];
While (Low < High)
{
While (Low < High && L [High] > = Pivotkey)
-- High;
L [low] = L [High];
While (Low < High && L [low] <= Pivotkey)
++ Low;
L [High] = L [low];
}
L [low] = L [ 0 ];
Return Low;
}
first obtain the Pivot Position, then partition the sequence lower than the Pivot Position (also a sorting process), and then perform the same operation on the sequence higher than the Pivot Position. The sequence above. In the sequence after the first partition, the elements [1] to [8] become 27, 38, 13, 49, 76, 97, 65, 49. 4 is the pivot position. Obviously, element [4] is earlier than 49, and key values after [4] are greater than or equal to 49. The partition process is also carried out for the elements [1]... [4] and [5]... [8. This recursion means that when low is not less than high, two consecutive elements are compared. Returns recursively.