【数据结构】堆与堆排序
2016-12-15 15:38
218 查看
//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(); }
相关文章推荐
- 数据结构之堆排序
- 复习数据结构:排序算法(六)——堆排序
- 【数据结构练习】排序——堆排序和快排
- 【数据结构】排序算法:希尔、归并、快速、堆排序
- 数据结构之排序算法二:堆排序,快速排序,归并排序
- 【数据结构&&算法系列】堆排序简单介绍及其实现
- 【数据结构】堆,堆实现优先级队列,堆排序
- ACM入门(2)——数据结构——堆排序
- 堆排序--数据结构排序1
- 【算法与数据结构】图说堆排序
- 数据结构(5)优先级队列与堆排序
- python数据结构学习笔记-2016-12-03-01-堆排序
- 堆(数据结构)及堆排序
- 数据结构例程——选择排序之堆排序
- 第16周SHH数据结构-【项目1-验证算法(6)堆排序 】
- 数据结构——堆排序
- 【数据结构系列】堆与堆排序
- 一步一步复习数据结构和算法基础-堆排序
- 【算法与数据结构】图说堆排序
- 数据结构复习之–“堆排序”-JAVA实现