#include <iostream>
using namespace std;
//......................获取支点索引..............................//
int KeyPivot(int List[], int i,int j)
{
int x = List[i];//x中始终存放支点的值,首先支点KeyPivot在数组的第一个元素i位置
while (i<j)
{
while ((i<j) && (x<=List[j]))
{
j--;
}
if (i<j)//即x>List[j],支点由i改为j
{
List[i] = List[j];
i++;
}
while ((i<j) && (x>=List[i]))
{
i++;
}
if (i<j)//即x<List[j],支点由j改为i
{
List[j] = List[i];
j--;
}
}
List[i]=x;//i=j时退出循环,所有元素都比较完,找到支点位置
return (i);
}
//............................QuickSort递归进行排序..........................//
void QuickSort(int List[],int startIndex,int lastIndex)
{
int key;//key是索引
if (startIndex < lastIndex)
{
key = KeyPivot(List,startIndex,lastIndex);
QuickSort(List,startIndex,key-1);
QuickSort(List,key+1,lastIndex);
}
}
int main()
{
int arr[8] = {49,38,65,97,76,13,27,69};
int i;
/*cout << "put out numbers: " << endl;
for (i=0;i<8;i++)
{
cin >> arr[8];
}*/
cout << "output arrey before qsorting:" << endl;
for (i=0;i<8;i++)
{
cout << arr[i] << ",";
}
cout << endl;
QuickSort(arr,0,7);
cout << "output arrey after qsorting:" << endl;
for (i=0;i<8;i++)
{
cout << arr[i] << ",";
}
cout << endl;
return 0;
}