快速排序

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值