/* * description: 快速排序示例 * writeby: nick * date: 2012-10-23 16:16 * */ #include <iostream> using namespace std; void quicksort(int a[], int l, int r) { int mid = a[(l+r) / 2]; int lwalker = l, rwalker = r; while(lwalker < rwalker) { while(a[lwalker] < mid) lwalker++; while(a[rwalker] > mid) rwalker--; if(lwalker <= rwalker) { int tmp = a[lwalker]; a[lwalker] = a[rwalker]; a[rwalker] = tmp; lwalker++; rwalker--; } } if(lwalker < r) quicksort(a, lwalker, r); if(rwalker > l) quicksort(a, l, rwalker); } int main() { int a[10] = {0, 6,4,2,8,1,5,3,7,9}; quicksort(a, 0, 9); for(int i=0; i<10; i++) cout << a[i] << " "; return 0; }