Swap sort-quick sort (quick sort)

Source: Internet
Author: User

Basic idea:

1 Select a datum element, usually select the first element or the last element,

2 by a trip to sort the records to be sorted to split into two separate parts, some of which record element values are smaller than the value of the base element. The other part of the record has an element value larger than the Datum value.

3 The correct position of the datum element at this time after its orderly

4 and then the two parts of the record in the same way continue to sort, until the entire sequence ordered.

A quick sort is generally considered to be the best in the ranking method of the same order of magnitude (O (nlog2n)). But if the initial sequence is ordered or basically ordered by the key code, the fast sort is reduced to bubble sort. In order to improve it, the benchmark record is usually selected by the "three-way Method", which adjusts the center of the two endpoints of the sorted interval and the midpoint of three key codes to the Fulcrum record. A quick sort is an unstable sort of method.

 #include <iostream> #include <string> using namespace std;  
    void print (int a[], int n) {for (int j= 0; j<n; j + +) {Cout<<a[j] << "";  
} cout<<endl;
    } void Swap (int &a,int &b) {int tmp=a;
    A=b;
b=tmp;
    int partion (int a[],int low,int high) {int privotekey=a[low];
        while (Low < high) {while (A[high] > Privotekey) high--;
        Swap (A[low],a[high]);
        while (a[low]< Privotekey) low++;
    Swap (A[low],a[high]);
    } print (a,10);
return to Low; } void QuickSort (int a[],int low,int high) {if (Low < high) {int privotekey=partion (a,low,high);//each time the value of the low position
        As the Datum value QuickSort (a,low,privotekey-1);
    QuickSort (A,privotekey+1,high);  
    int main () {int a[10] = {3,1,5,7,2,4,9,6,10,8};  
    cout<< "initial value:";  
    Print (a,10);  
    QuickSort (a,0,9);  
    cout<< "Result:";  

Print (a,10); }  

Initial Value: 3 1 5 7 2 4 9 6 8
2 1 3 7 5 4 9 6 8
1 2 3 7 5 4 9 6 8 1 2 3 6 5 4 7
9 8 1 2 3 4 7 9 8
1 2 3 4 5 6 7 9 Ten 8
1 2 3 4 5 6 7 8 9 Ten
Results: 1 2 3 4 5 6 7 8-9

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.