#include"iostream"
using namespace std;
void Swaps1(int a[], int &low, int high){//右边的向左边赋值
a[low] = a[high];
low++;//必须自加,让其指向下一个位置
}
void Swaps2(int a[], int low, int &high){//左边向右边赋值
a[high] = a[low];
high--;
}
int partition(int a[],int low,int high){
int temp = a[low];
while (low<high){
//5, 47, 31, 4, 8, 61, 5, 10, 32, 15
while ((low<high) && (a[high]>temp)){
high--;
cout << "high=" << high << " low=" << low << endl;
}
Swaps1(a, low, high);//high的值向low的空间赋值。
while ((low < high) && (a[low]<temp)){
//cout << "值为" << (low < high) && (a[low] < temp);
low++;
cout << "high=" << high << " low=" << low << endl;
}
Swaps2(a, low, high);//low的值向high的空间赋值。
}
a[low] = temp;//找到最终的插入位置了。左边比它小,右边比它大!!!
return low;//low与high重合了
}
int quicksort(int a[],int low,int high){
if (low > high){
return -1;
}
int pivot = partition(a,low,high);
quicksort(a, low, pivot-1);//注意这是pivot-1;
quicksort(a, pivot+1, high);
return 0;
}
int main(){
int a[10] = { 5, 47, 31, 4, 8, 61, 5, 10, 32, 15 };
quicksort(a, 0, 9);
for (int i = 0; i < 10; i++){
cout << a[i] << " ";
}
system("pause");
return 0;
}