快速排序的分治求解方法

5

9 1 0 -2 6

-2 0 1 6 9

 

 

 

#include <iostream>
#include <algorithm>
using  namespace std;
int a[105];
int b[105];

int Partition(int data[],int low,int high)
{
    int temp = data[low];
    int pivotkey=data[low];
    while(low<high)
    {
        while(low<high && data[high]>=pivotkey)
            --high;
        data[low]=data[high];
        while(low<high && data[low]<=pivotkey)
            ++low;
        data[high]=data[low];
    }
    data[low] = temp;
    return low;
}

void QuickSort(int array[], int first, int last)
{
    
    if (first < last)
    {
        int i = Partition(array, first, last);
        QuickSort(array, first, i - 1);
        QuickSort(array, i + 1, last);
    }
}


int main()
{
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    QuickSort(a,1,n);
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

 

posted on 2018-03-21 17:43  蔡军帅  阅读(256)  评论(0编辑  收藏  举报