排序算法之堆排序(附源码)

1、将顺序存储的数据看成是一颗完全二叉树
2、对于大顶堆,确保每棵子树的根节点都是整个子树中的最大值;这就保证了根节点是所有数据中的最大值,但不保证所有数据有序。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/*
*************** achieve heap sorting*********************
*/
#include<iostream>
//#include<ctime>
//#include<algorithm>
using namespace std;

//调整子树为大顶堆结构
void heapAdjust(int data[], int start, int end)
{
	/*************方式一********************
	//int max = start;
	//int tmp = data[max];
	while (2 * start <= end)
	{
		int max = start;
		int left = 2 * start;
		int right = left + 1;
		
		if (data[max] < data[left] && left <= end)
		{
			max = left;
		}
		if (data[max] < data[right] && right <= end)
		{
			max = right;
		}
		if (max != start)
		{
			swap(data[start], data[max]);
			start = max;
		}
		else
		{
			break;
		}
	}*/


	/***********方式二*************/
	int tmp = data[start];
	for (size_t i = start*2; i <= end; i*=2)
	{
		//找出最大子节点
		if (i < end && data[i]<data[i+1])
		{
			++i;
		}
		if (tmp>=data[i])
		{
			break;
		}

		//令父节点等于最大子节点
		data[start] = data[i];
		start = i;//改变父节点序号
	}
	data[start] = tmp;
	
}

void heapSort(int data[], int start, int end)//变量 end 能表示长度
{
	//构造大顶堆
	for (size_t i = end/2; i > 0; i--)
	{
		heapAdjust(data, i, end);
	}

	//交换末尾元素与根节点元素(最大值),并对根节点进行调整,使其变成大顶堆
	for (size_t i = end; i > 1; i--)
	{
		swap(data[1], data[i]);
		heapAdjust(data, 1, i-1);
	}
}
int main()
{
	//测试用例一
	int b[9] = { -1,49,38,65,97,76,13,27,49 };
	for (size_t i = 1; i < 9; i++)
	{
		cout << b[i] << " ";
	}
	cout << endl;

	heapSort(b, 1, 8);

	for (size_t i = 1; i < 9; i++)
	{
		cout << b[i] << " ";
	}
	cout << endl;

	//测试用例二
	int a[11] = {-1, 7,3,2,6,9,1,2,0,6,4 };
	for (size_t i = 1; i < 11; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;

	heapSort(a, 1, 10);

	for (size_t i = 1; i < 11; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;
}

STL中优先队列的应用
//堆实现优先队列

//Algo2-10.cpp STL中优先队列的应用   
#include<iostream>
#include<queue>
#include<vector>

using namespace std;

struct PosType
{
	int row;
	int col;
	
};


bool operator<(const PosType a, const PosType b)
{
	return a.row < b.row; //降序排列
}
bool operator>(const PosType a, const PosType b)
{
	return a.row > b.row; //升序排列
}/**/
class cmp
{
public:
	bool operator()(const PosType a, const PosType b)const
	{
		return a.row > b.row;
		//return a.row<b.row;//按PosType的row域降序排序
	}
};

void test1()
{
	PosType p[] = { {3, 4}, {5, 6}, {4, 1} }, q;
	//	priority_queue<PosType, vector<PosType>, cmp> pri_queue;
	priority_queue<PosType, deque<PosType>, cmp> pri_queue;//容器为双端队列

	for (int i = 0; i < 3; i++)
		pri_queue.push(p[i]);
	cout << "优先队列元素数=" << pri_queue.size() << endl;
	while (!pri_queue.empty())
	{
		q = pri_queue.top();
		cout << '(' << q.row << ", " << q.col << ") ";
		pri_queue.pop();
	}
	cout << endl;
	//return 0;
}

void test2()
{
	PosType p[] = { {3, 4}, {5, 6}, {4, 1} }, q;
	priority_queue<PosType, vector<PosType>, greater<PosType>> pri_queue; //升序
	for (int i = 0; i < 3; i++)
		pri_queue.push(p[i]);
	cout << "优先队列元素数=" << pri_queue.size() << endl;
	while (!pri_queue.empty())
	{
		q = pri_queue.top();
		cout << '(' << q.row << ", " << q.col << ") ";
		pri_queue.pop();
	}
	cout << endl;
}
  
void test3()
{
	int p[] = { 3,5,4 },q;
	//priority_queue<int, vector<int>, greater<int>> pri_queue; //升序
	priority_queue<int, vector<int>, less<int>> pri_queue; //降序
	for (int i = 0; i < 3; i++)
		pri_queue.push(p[i]);
	cout << "优先队列元素数=" << pri_queue.size() << endl;
	while (!pri_queue.empty())
	{
		q = pri_queue.top();
		cout << q<<" ";
		pri_queue.pop();
	}
	cout << endl;
}

int main()
{
	test1();
	test2();
	test3();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值