(9) Fast sorting of the second Exchange Order

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.