快速排序(算法导论版)
#include using namespace std;void swap(int &a,int &b){ int temp = a; a = b; b = temp;}int parttion(int a[],int l,int r){ int x = a[r]; //取数组最后一个元素为基准值 int i = l - 1; //比基准值小的数组的窗口数组最大下标 for(int j = l;j <= r-1;j++){ if(a[j] <= x){ i = i + 1; swap(a[i],a[j]); } } swap(a[i+1],a[r]); return i+1;}void quick_sort(int a[],int l,int r){ if(l < r){ int q = parttion(a,l,r); quick_sort(a,l,q-1); quick_sort(a,q,r); }}int main(void){ int a[8] = {2,8,7,1,3,5,6,4}; quick_sort(a,0,7); cout<<"排序后的值为:\n"; for(int i = 0;i < 8;i++){ printf("%d ",a[i]); } return 0;}