void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } int partition(int data[], int lo, int hi)//data[lo]--data[hi] { int i, j; int key = data[hi]; i = lo; for(j = lo; j < hi ; j++) { if (data[j] <= key) { swap(&data[i], &data[j]); i++; } } swap(&data[i],&data[hi]); return i; } void QuickSort(int data[], int lo, int hi) { int k; if(lo<hi) { k = partition(data, lo, hi); QuickSort(data,lo,k-1); QuickSort(data,k+1,hi); } } int main() { int data[] = {3,2,7,4,9,1,8,5,5}; QuickSort(data,0,8); for(int i = 0; i < 9; i++) cout<<*(data+i)<<" "; cout<<endl; }
原文:http://blog.csdn.net/gaoxiangky/article/details/19766809