#include <iostream>
using namespace std;
void Show(int* pList, int nCount)
{
for (int nIndex = 0; nIndex < nCount; nIndex++)
cout << pList[nIndex] << "\t";
cout << endl;
}
/*
划分函数
@pList:待划分的数组
@nHead:头坐标
@nEnd:尾坐标
返回划分标准元素的坐标
*/
int Partition(int* pList,int nHead,int nEnd)
{
//将头元素作为划分元素
int nSave = pList[nHead];
while (nHead<nEnd)
{
//从后往前查找第一个小于划分元素的元素坐标!
while (pList[nEnd] >= nSave && nHead < nEnd)
nEnd--;
//找到后移动元素和加一
if (nHead < nEnd)
pList[nHead++] = pList[nEnd];
//从前往后找到第一个大于划分元素的元素坐标
while (pList[nHead] <= nSave && nHead < nEnd)
nHead++;
//找到后移动元素和加一
if (nHead < nEnd)
pList[nEnd--] = pList[nHead];
}
pList[nHead] = nSave;
return nHead;
}
void QuickSort(int* pList,int nHead,int nEnd)
{
if (nHead < nEnd)//递归的终止条件
{
int nIndex = Partition(pList, nHead, nEnd);//划分函数
QuickSort(pList, nHead, nIndex - 1);//前部分排序
QuickSort(pList, nIndex + 1, nEnd);//后部分排序
}
}
int main(int argc, char* argv[], char* enp[])
{
int pList[] = {15,78,95,65,35,12,47,55,22,77,};
int nCount = sizeof(pList) / sizeof(int);
QuickSort(pList, 0, 9);
Show(pList, nCount);
system("pause");
return 0;
}