算法:把未排序的数据一个一个放入堆里,然后再一个一个的取出来。
我们今天使用上一个博客写的大顶堆MaxHeap.h
//MaxHeap.h
#ifndef _MAX_HEAP_
#define _MAX_HEAP_
template<class T>
class MaxHeap{
public:
MaxHeap(int mx = 10);
virtual ~MaxHeap();
bool IsEmpty();
void Push(const T&);
void Pop();
const T& Top() const;
private:
T* heapArray;
int maxSize;
int currentSize;
void trickleUp(int index);
void trickleDown(int index);
};
template<class T>
MaxHeap<T>::MaxHeap(int mx = 10){
if (mx < 1) throw "max size must be >= 1";
maxSize = mx;
currentSize = 0;
heapArray = new T[maxSize];
}
template<class T>
MaxHeap<T>::~MaxHeap(){
delete[]heapArray;
}
template<class T>
bool MaxHeap<T>::IsEmpty(){
return currentSize == 0;
}
template <class T>
void MaxHeap<T>::Push(const T&e){
if (currentSize == maxSize) throw "MaxHeap id full.";
heapArray[currentSize] = e;
trickleUp(currentSize);
currentSize++;
}
template<class T>
void MaxHeap<T>::trickleUp(int index){
int parent = (index - 1) / 2;
T bottom = heapArray[index];
while (index>0 && heapArray[parent] < bottom){
heapArray[index] = heapArray[parent];
index = parent;
parent = (parent - 1) / 2;
}
heapArray[index] = bottom;
}
template<class T>
const T& MaxHeap<T>::Top()const{
return heapArray[0];
}
template<class T>
void MaxHeap<T>::Pop(){
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
}
template<class T>
void MaxHeap<T>::trickleDown(int index){
int largerChild;
T top = heapArray[index];
while (index < currentSize / 2){
int leftChild = 2 * index + 1;
int rightChild = leftChild + 1;
if (rightChild < currentSize && heapArray[leftChild] < heapArray[rightChild])
largerChild = rightChild;
else
largerChild = leftChild;
if (top >= heapArray[largerChild])
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
#endif
下面是我堆排序的主程序main.cpp
//main.cpp
#include<iostream>
#include"MaxHeap.h"
using namespace std;
int main(){
cout << "测试堆:" << endl;
MaxHeap<int> h(100);
int arr[] = { 62, 3, 90, 27, 33, 8, 12, 9, 43, 66 };
for (int i = 0; i < 10; i++)
h.Push(arr[i]);
for (int i = 0; i < 10; i++){
arr[i] = h.Top();
h.Pop();
}
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
system("pause");
return 0;
}
总结:堆排序比较简单就是把数据一个一个push进堆,再一个一个从堆里取出来。