快排中 partition 的两种写法!
1
void swap(int &A,int &B) { int temp; temp=A; A=B; B=temp; } int partition(QVector<int> &numbers,int low,int high) { int pivotkey=numbers[low];//当参考值 while(low<high) { while(low<high&&numbers[high]>pivotkey) {high--;} swap(numbers[low],numbers[high]); while(low<high&&numbers[low]<=pivotkey) { low++;} swap(numbers[low],numbers[high]); } return low; }
2
int partition(QVector<int> &input, int s, int e){ int j = s-1; int cmp = input[e]; for (int i = s; i < e; ++i) { if (input[i] < cmp) swap(input[i], input[++j]); } swap(input[e], input[++j]); }