为什么用递归写的快排超时???难道非得逼我用迭代版的快排吗???
查看原帖
为什么用递归写的快排超时???难道非得逼我用迭代版的快排吗???
706459
Go_for_itligli666666楼主2022/4/10 22:29
#include<iostream>
using namespace std;
typedef long long inty;
void QuickSort(inty a[],int start,int end){
    if(start>=end){
        return;
    }
    int x=start,y=end;
    inty base=a[start];
    while(x<y){
        while(a[y]>base&&y>x){
            y--;
        }
        if(y>x){
            a[x++]=a[y];
        }
        while(a[x]<base&&y>x){
            x++;
        }
        if(y>x){
            a[y--]=a[x];
        }
    }
    a[x]=base;
    QuickSort(a,start,x-1);
    QuickSort(a,x+1,end);
}
int main()
{
    inty N;
    cin>>N;
    inty a[100000];
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    QuickSort(a,0,N-1);
    for(int i=0;i<N;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;//最后一个要不要空格?
    return 0;
}
2022/4/10 22:29
加载中...