快速排序 c++代码

来源:互联网 发布:丝路英雄嘉峪关数据 编辑:程序博客网 时间:2024/06/07 00:25
#include <iostream>


using namespace std;


int partition(int *l,int low,int high){
    while(low<high){
       while(low<high&&l[high]>=l[low]){
          high--;
       }
       swap(l[low],l[high]);
       while(low<high&&l[low]<=l[high]){
          low++;
       }
       swap(l[low],l[high]);
    }
    return low;
}
void qkSort(int *l,int low,int high){
    if(low<high){
        int p=partition(l,low,high);
        qkSort(l,low,p-1);
        qkSort(l,p+1,high);
    }
}
const int maxn=1000;
int a[maxn];
int main()
{
    int n;
    while(cin>>n){
       for(int i=0;i<n;i++)scanf("%d",&a[i]);
       qkSort(a,0,n-1);
       for(int i=0;i<n;i++)
         printf("%d ",a[i]);
    }
    return 0;
}
0 0