#include <stdio.h> #include <stack> using namespace std; void exch(int& a, int &b) { int tmp = a; a = b; b = tmp; } int partition(int a[], int l, int r) { int i=l-1; int j=r; int val = a[r]; while(1) { while(val > a[++i]); while(val < a[--j]) if(j == l) break; if(i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i; } void quicksort(int a[], int l, int r) { if(l >= r ) return; int k; int v = a[r]; int i=l-1, j=r; int p=l-1, q=r; while(1) { while(a[++i]<v); while(a[--j]>v) if(j == l) break; if(i>=j) break; exch(a[i], a[j]); if(a[i]== v) { p++; exch(a[p], a[i]); } if(a[j]== v) { q--; exch(a[q], a[j]); } } exch(a[i], a[r]); j = i-1; i = i+1; for(k=l; k<=p; k++, j--) exch(a[k], a[j]); for(k=r-1; k>=q; k--,i++) exch(a[k], a[i]); quicksort(a, l, j); quicksort(a, i, r); } int main() { int a[10] = {4,3,6,5,3,1,8,9,6,5}; quicksort(a, 0, 9); for(int i=0; i<10; i++) printf("%d ", a[i]); printf("\n"); return 0; }