快排

emmm,这东西也很简单

就是对于l,r把第一个数放到他应该放的位置

再把l,r分成两个区间再进行排序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 10000

using namespace std;

int a[maxn];

void Swap(int&a,int&b)
{
    if (a==b)
        return;//这个是为了防止a和b都是a[i]的情况,这种情况下,会把a[i]变成0
    a^=b;b^=a;a^=b;
    return;
}

void qsort(int l,int r)
{
    if (l>r)
        return;

    int save = a[l],low=l,high=r;

    while(low!=high)
    {
        while(a[high]>=save&&low<high)
            high--;

        while(a[low]<=save&&low<high)
            low++;

        if (low<high)
            Swap(a[low],a[high]);
    }

    Swap(a[l],a[low]);

    qsort(l,low-1);
    qsort(low+1,r);
    return;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for (int k=1;k<=n;k++)
            scanf("%d",&a[k]);

        qsort(1,n);

        for (int k=1;k<=n;k++)
            printf("%d ",a[k]);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shensobaolibin/p/8109537.html