【数据结构】堆与堆排序

//heap.h
#pragma once
#include<iostream>

using namespace std;

class Heap {//静态实现堆
private:
	int count;  //堆中元素个数
	int capacity; //堆的大小
	int *array; //数组实现堆
	int heap_type; //最小堆或最大堆,0为最小,1为最大
public:
	Heap(int capacitay, int heap_type);//堆得容量,堆得类型
	~Heap();
	int Parents(int i);//得到父亲下标
	int LeftChild(int i);//左孩子
	int RightChild(int i);//右孩子
	int GetMaximum();//得到堆头
	void PercolateDown(int i);//向下堆化
	int Delete();
	void Resize();
	int Insert(int i);//插入
	void Build(Heap &H, int a[], int n);//利用现有数组构建堆
	void Heapsort();//堆排序
};
//heap.cpp
#include"heap.h"

Heap::Heap(int capacitay, int heap_type)
{
	this->heap_type = heap_type;
	this->capacity = capacitay;
	this->array = new int[capacitay];
	this->count = 0;
	cout << "Creation is OK~" << endl;
}

Heap::~Heap()
{
	this->count = 0;
	this->array = NULL;
}

int Heap::Parents(int i)
{
	if (i <= 0 || i >= this->count)
		return -1;
	return i - 1 / 2;
}

int Heap::LeftChild(int i)
{
	int left = 2 * i + 1;
	if (left >= this->count)
		return -1;
	return left;
}

int Heap::RightChild(int i)
{
	int right = 2 * i + 2;
	if (right >= this->count)
		return -1;
	return right;
}

int Heap::GetMaximum()
{
	if (this->count == 0)
		return -1;
	return this->array[0];
}

void Heap::PercolateDown(int i)//向下渗透
{
	int max, temp;
	int l = LeftChild(i);
	int r = RightChild(i);
	if(this->heap_type==0)
	{	if (l != -1 && this->array[l] > this->array[i])
		max = l;
		else
		max = i;
		if (r != -1 && this->array[r] > this->array[max])
			max = r; 
		if (max != i)
		{
			temp = this->array[i];
			this->array[i] = this->array[max];
			this->array[max] = temp;
			PercolateDown(max);
		}
	}
	else
	{
		if (l != -1 && this->array[l] < this->array[i])
			max = l;
		else
			max = i;
		if (r != -1 && this->array[r] < this->array[max])
			max = r;
		if (max != i)
	{
		temp = this->array[i];
		this->array[i] = this->array[max];
		this->array[max] = temp;
		PercolateDown(max);
	}
	}


	
}

int Heap::Delete()
{
	if (this->count == 0)
		return -1;
	int top = this->array[0];
	this->array[0] = this->array[this->count - 1];
	this->count--;
	PercolateDown(0);
	return top;
}

void Heap::Resize()
{
	int *array_old = new int[this->capacity];
	for (int i = 0; i < this->count; i++)
		array_old[i] = array[i];
	this->array = new int[this->capacity * 2];
	for (int i = 0; i < this->count; i++)
		array[i] = array_old[i];
	this->capacity *= 2;
	array_old = NULL;
}

int Heap::Insert(int i)
{
	int temp;
	if (this->capacity == this->count)
		Resize();
	this->count++;
	temp = this->count - 1;
	while(i >= 0 && i>this->array[(temp-1)/2])
	{
		this->array[temp] = this->array[(temp - 1) / 2];
		temp = temp - 1 / 2;
	}
	this->array[temp] = i;
	return 1;
}

void Heap::Build(Heap &H, int a[], int n)
{
//	if (H.array == NULL)
	//	return;
	while (n > H.capacity)
		H.Resize();
	for (int i = 0; i < n; i++)
		H.array[i] = a[i];
	H.count = n;
	for (int i = (n - 2) / 2; i >= 0; i--)
		H.PercolateDown(i);
}

void Heap::Heapsort()//堆排序
{
	while (this->count > 0) {
		cout << this->GetMaximum();
		this->Delete();
	}
}



//main.cpp
#include"heap.h"


void main()//最小堆
{
	int a[8] = {};
	int type,i;
	for (i = 0; i < 8;i++)
	{ cin >> a[i];}
	cin >> type;
	Heap H(6,type);
	H.Build(H, a, 8);
	H.Heapsort();


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值