Quick Sort

#include<iostream>
using namespace std;

const int MAX = 10001;
int l[MAX];

int Partition(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;
}

void QSort(int low, int high)
{
    if(low<high)
    {
       int pivotkey = Partition(low, high);
       QSort(low, pivotkey-1);
       QSort(pivotkey+1, high);
    }
}

int main()
{
    int n;
    cin>>n;

    for(int i=1;i<=n;i++)
        cin>>l[i];

    QSort(1, n);

    for(int i=1;i<=n;i++)
        cout<<l[i]<<" ";

    cout<<endl;

    return 0;
}

  

posted @ 2017-01-05 09:50  KennyRom  阅读(155)  评论(0编辑  收藏  举报