コンテンツへスキップ

#include 
#include 
#include 

#define S(a) (sizeof(a)/sizeof(a[0]))

int x[] = { 56, 10, 39, 15, 20, 62, 14, 39, 77, 17 };

void swap(int i, int j)
    {
    int t = x[i];
    x[i] = x[j];
    x[j] = t;
    }


void quicksort(int l, int u)
    {
    int i, m;
    if (l >= u) return;
    swap(l, randint(l,u));
    m = l;
    for ( i = l+1; i <= u; i++)
        if (x[i] < x[l])
            swap(++m, i);
    swap(l,m);
    quicksort(l, m-1);
    quicksort(m+1,u);
    }

int randint(int l, int u) {
    return rand()%(u-l+1)+l;
}

int main(void)
    {
    int i;
    srand(time(NULL));
    quicksort(0,S(x)-1);
    for (i = 0; i < S(x); i++)
        {
        printf("%d ", x[i]);
        }
    printf("\n");
    return 0;
    }