快排代码


#include <iostream>

using namespace std;

int a[100005];

void sort(int l, int r){

    cout<<l<<" "<<r<<endl;
    if(l>=r)return;

    int anchor = a[l];
    int p1 = l;
    int p2 = r;

    while(p1<p2){
        while(p1<p2&&a[r]>=anchor)--p2;
        if(p1<p2)a[p1]=a[p2];
        while(p1<p2&&a[l]<=anchor)++p1;
        if(p1<p2)a[p2]=a[p1];
    }

    a[p1]=anchor;

    sort(l,p1-1);
    sort(p2+1,r);

}

int main(){

    a[1] = 10;
    a[2] = 9;
    a[3] = 8;
    a[4] = 7;
    a[5] = 6;
    a[6] = 5;
    a[7] = 4;
    a[8] = 3;
    a[9] = 2;
    a[10] = 1;

    sort(1, 10);

    for(int i = 1; i <= 10; ++i){
        cout<<a[i]<<" ";
    }
    cout<<endl;

    return 0;
}```
posted @ 2023-09-12 20:16  John_Ran  阅读(13)  评论(0编辑  收藏  举报