好吧,我也可以裸写快排了.

#include 

int partition(int a[], int p, int r)
{
    int x = a[r];
    int i = p-1;
    int tmp = 0;
    for (int j = p; j < r; ++j) {
        if (a[j] <= x) {
            ++i;
            tmp = a[j];
            a[j] = a[i];
            a[i] = tmp;
        }
    }
    ++i;
    a[r] = a[i];
    a[i] = x;
    return i;
}

void qsort(int a[], int p, int r)
{
    if (p < r) {
        int q = partition(a, p ,r);
        qsort(a, p, q-1);
        qsort(a, q+1, r);
    }
}

int main()
{
    int a[10] = {999,2,8,7,1,3,6,5,4};
    
    qsort(a,1,8);
    
    for (int i = 1; i <= 8; ++i)
        printf("%d ", a[i]);
        
    while (true);
    return 0;
} 

你可能感兴趣的:(algorithm)