堆排序

#include <iostream>
#include<cstdio>

using namespace std;

int Heap[100];
int Hlength;

void max_heapify(int Heap[], int s, int m)        //向下调整堆
{
	int rc = Heap[s];
	for (int j = s * 2; j <= m; j *= 2)
	{
		if (j < m&&Heap[j] < Heap[j + 1])	j++;
		if (Heap[j] <= rc)	break;
		Heap[s] = Heap[j];
		s = j;
	}
	Heap[s] = rc;
}

void up_adjust(int heap[], int s)        //向上调整堆,用于插入元素和升级优先级
{
	int rc = heap[s];
	for (int p = s / 2; p > 0 && heap[p] < rc; p /= 2)
	{
		heap[s] = heap[p];
		s = p;
	}
	heap[s] = rc;
}

void increase_key(int heap[], int s, int k)		//将heap[s]的优先级升为k
{
	if (k < heap[s])	return;
	heap[s] = k;
	up_adjust(heap, s);
}

void insert(int heap[],int k)        //插入元素
{
	heap[++Hlength] = k;       
	up_adjust(heap, Hlength);
}

int deleteMax(int heap[])        //删除最大值
{
	int max = heap[1];
	heap[1] = heap[Hlength--];
	max_heapify(heap, 1, Hlength);
	return max;
}

void build_max_heap(int heap[])        //建堆
{
	for(int i = Hlength / 2; i > 0; i--)
		max_heapify(heap, i, Hlength);
}

void heapsort(int heap[])        //堆排序
{
	build_max_heap(heap);
	for (int i = Hlength; i > 1; i--)
	{
		int temp = heap[1];
		heap[1] = heap[i]; heap[i] = temp;
		max_heapify(heap, 1, i - 1);
	}
}
void print_heap(int heap[])
{
	for (int i = 1; i <= Hlength; i++)
	{
		printf("%-3d",heap[i]);
	}
	cout << endl;
}
int main()
{
	int num,i;
	cin >> num;
	while (num--)
	{
		Hlength = 0;
		while (cin >> i, i > 0)
			Heap[++Hlength] = i;
		build_max_heap(Heap);
		cout << "建堆后:" << endl;
		print_heap(Heap);
		cout << "插入元素6:" << endl;
		insert(Heap, 6);
		print_heap(Heap);
		cout << "Heap[3]增大为8:" << endl;
		increase_key(Heap, 3, 8);
		print_heap(Heap);
		heapsort(Heap);
		cout << "堆排序:" << endl;
		print_heap(Heap); 
	}
	return 0;
}

测试结果:


头文件<algorithm>中有关堆排序的函数有make_heap(),push_heap(),pop_heap(),sort_heap(),都有两个构造函数。第一个构造函数有两个参数:容器的起点begin和终点end,形成大顶堆。第二个构造函数除了前两个迭代器参数,第三个参数是排序准则。

1.建立堆:make_heap(_RanIt _First,_RanIt _Last,_Pr _Pred)

默认建立大顶堆,对int类型,可以在第三个参数传入greater<int>()  得到小顶堆。

2.向堆中添加数据:push_heap(_RanIt _First,_RanIt _Last,_Pr _Pred)

只能基于已经是堆的容器添加数据。要先在容器中添加数据,再调用push_heap()。

3.删除堆顶数据:pop_heap(_RanIt _First,_RanIt _Last,_Pr _Pred)

删除第一个数据,重新排列成堆。先调用pop__heap(),再在容器中删除数据

4.堆排序:sort_heap(_RanIt _First,_RanIt _Last,_Pr _Pred)

默认升序排列,必须作用于堆。

#include<iostream>  
#include<algorithm>  
#include<vector>  
using namespace std;  
  
void print(int x)  
{  
    cout << x << ' ';  
}  
int main()  
{  
    vector<int>v;  
    for(int i=0;i<10;i++)  
        v.push_back(i);  
  
    cout << "创建堆" << endl;  
    make_heap(v.begin(), v.end());  
    for_each(v.begin(), v.end(), print);  
  
    cout <<endl<<"向堆中添加数据 6"<<endl;  
    v.push_back(6); push_heap(v.begin(), v.end());  
    for_each(v.begin(), v.end(), print);  
  
    cout <<endl<<"删除堆顶数据:" << endl;  
    pop_heap(v.begin(), v.end());  
    for_each(v.begin(), v.end(), print);  
    v.pop_back();  
  
    cout <<endl<< "进行堆排序" << endl;  
    sort_heap(v.begin(), v.end());  
    for_each(v.begin(), v.end(), print);  
    getchar();  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值