#include <iostream>
using namespace std;
void print(int data[],int len);
void quick_sort(int data[],int low,int high)
{
int p,q,iTemp;
if(low >= high)
return;
p = low;
q = high;
iTemp = data[low];//设定第一个数据为中间判断值
while(p < q){
while(p < q && data[q] > iTemp){
q--;
}
if(p < q)
data[p++] = data[q];//将p后移一个位置,此位置已排好
while(p < q && data[p] < iTemp){
p++;
}
if(p < q)
data[q--] = data[p];//将q前移一个位置,此位置已排好
}
data[p] = iTemp;//中间值,已经排好序
#ifdef _DEBUG //调试用
print(data,10);
cout << "low:" << low << ",high:" << high << ",p:" << p << endl;
#endif
quick_sort(data,low,p-1);//p位置已经排好序,不需要再进入下一轮排序
quick_sort(data,p+1,high);//p位置已经排好序,不需要再进入下一轮排序
}
void print(int data[],int len)
{
for(int i = 0;i < len;i++){
cout << data[i] << " ";
}
cout << endl;
}
int main()
{
int data[] = {4,2,3,7,6,1,5,9,10,8};
print(data,10);
cout << "###################" << endl;
quick_sort(data,0,sizeof(data)/sizeof(int)-1);
cout << "###################" << endl;
print(data,sizeof(data)/sizeof(int));
cout << "###################" << endl;
return 0;
}