1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdio.h>
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
void quick_sort(int a[], int l, int r) { if (l < r) { int i,j,x;
i = l; j = r; x = a[i]; while (i < j) { while(i < j && a[j] > x) j--; if(i < j) a[i++] = a[j]; while(i < j && a[i] < x) i++; if(i < j) a[j--] = a[i]; } a[i] = x; quick_sort(a, l, i-1); quick_sort(a, i+1, r); } }
void main() { int i; int a[] = {30,40,60,10,20,50}; int ilen = LENGTH(a);
printf("before sort:"); for (i=0; i<ilen; i++) printf("%d ", a[i]); printf("\n");
quick_sort(a, 0, ilen-1);
printf("after sort:"); for (i=0; i<ilen; i++) printf("%d ", a[i]); printf("\n"); }
|