八大排序之快速排序-双向划分

递归实现:

int Parition(int* br, int left, int right)
{
	int tmp = br[left];
	while (left < right)
	{
		while (left<right && br[right]>tmp)
		{
			--right;
		}
		if (left < right)br[left] = br[right];
		while (left < right && br[left] <= tmp)
		{
			++left;
		}
		if (left < right)br[right] = br[left];
	}
	br[left] = tmp;
	return left;
}
void PassQuick(int* br, int left, int right)
{
	if (left < right)//不能是<=,确保有两个元素
	{
		int pos = Parition(br, left, right);
		PassQuick(br, left, pos);
		PassQuick(br, pos + 1, right);
	}
}
void QuickSort(int* br, int n)
{
	if (br == NULL || n < 2)return;
	PassQuick(br, 0, n - 1);
}

非递归–队或栈实现:

void QuickSort(int* br, int n)
{
	if (br == NULL || n < 2)return;
	stack<int> qu;
	qu.push(0);
	qu.push(n - 1);
	while (!qu.empty())
	{
		int left = qu.front; qu.pop();
		int right = qu.front; qu.pop();
		int pos = Parition(br, left, right);
		if (left < pos - 1)
		{
			qu.push(left);
			qu.push(pos - 1);
		}
		if (pos + 1 < right)
		{
			qu.push(pos + 1);
			qu.push(right);
		}
	}
}

非递归–对实现

void QuickSort(int* br, int n)
{
	if (br == NULL || n < 2)return;
	queue<std::pair<int, int>>qu;
	qu.push(std::pair<int, int>(0, n - 1));
	while (!qu.empty())
	{
		std::pair<int, int>tmp = qu.front();qu.pop();
		int pos = Parition(br, tmp.first,tmp.second);
		if (tmp.first < pos - 1)
		{
			qu.push(std::pair<int, int>(tmp.first, pos - 1));
		}
		if (pos + 1 < tmp.second)
		{
			qu.push(std::pair<int, int>(pos + 1, tmp.second));
		}
	}
}

//改进
void QuickSort(int* br, int n)
{
	if (br == NULL || n < 2)return;
	typedef std::pair<int, int>Pair;
	queue<Pair>qu;
	qu.push(Pair(0, n - 1));
	while (!qu.empty())
	{
		std::pair<int, int>tmp = qu.front(); qu.pop();
		int pos = Parition(br, tmp.first, tmp.second);
		if (tmp.first < pos - 1)
		{
			qu.push(Pair(tmp.first, pos - 1));
		}
		if (pos + 1 < tmp.second)
		{
			qu.push(Pair(pos + 1, tmp.second));
		}
	}
}

快排特点:越乱越快,越有序越慢
解决方法如下:
快速排序优化一随机化法:在范围内随机找一个数,和left进行交换

int RandParition(int* br, int left, int right)
{
	srand(time(NULL));//防止随机数一样
	int  pos = rand() % (right - left + 1) + left;
	std::swap(br[left], br[pos]);
	return Parition(br, left, right);
}
void QuickSort(int* br, int n)
{
	if (br == NULL || n < 2)return;
	queue<std::pair<int, int>>qu;
	qu.push(std::pair<int, int>(0, n - 1));
	while (!qu.empty())
	{
		std::pair<int, int>tmp = qu.front();qu.pop();
		int pos = RandParition(br, tmp.first,tmp.second);
		if (tmp.first < pos - 1)
		{
			qu.push(std::pair<int, int>(tmp.first, pos - 1));
		}
		if (pos + 1 < tmp.second)
		{
			qu.push(std::pair<int, int>(pos + 1, tmp.second));
		}
	}
}

快速排序优化一三位取中法:left和right还有范围内的中间值,取这三个数中的中间值

int MidParition(int* br, int left, int right)
{
	int mid = (right - left) / 2 + left;
	struct IndexNode
	{
		int key;
		int index;

		operator int()const//强转为int
		{
			return key;
		}
	};
	struct IndexNode kL = { br[left],left };
	struct IndexNode kM = { br[mid],mid };
	struct IndexNode kR = { br[right],right };
	std::priority_queue<IndexNode>hp;
	hp.push(kL); hp.push(kM); hp.push(kR);
	hp.pop();
	struct IndexNode pos = hp.top();
	std::swap(br[kL.index], br[pos.index]);
	return Parition(br, left, right);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值