- #include <cstdio>
- #define ARRLEN(x) (sizeof(x)/sizeof(int))
- int partition(int a[], int low, int high)
- {
- int pivotkey=a[low];
- while (low<high)
- {
- while (low<high && a[high]>=pivotkey)
- --high;
- if (low<high)
- a[low++]=a[high];
- while (low<high && a[low]<=pivotkey)
- ++low;
- if (low<high)
- a[high--]=a[low];
- }
- a[low]=pivotkey;
- return low;
- }
- void quicksort(int a[], int low, int high)
- {
- int pivotpos;
- if (low<high)
- {
- pivotpos=partition(a,low,high);
- quicksort(a,low,pivotpos-1);
- quicksort(a,pivotpos+1,high);
- }
- }
- int main()
- {
- int a[]={2,7,9,3,1,4,8,0,6,2};
- int i;
- for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);
- printf("/n");
- quicksort(a,0,ARRLEN(a)-1);
- for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);
- printf("/n");
- return 0;
- }