摘自《数据结构》(机械工业出版社)。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
对一个根节点的一次调整过程。
*/
void MakeHeap(vector<int>& DataArray, int startIndex, int Size)
{
int parentIndex = startIndex; //根
int childIndex = 2 * parentIndex + 1; //左孩子
int parentValue = DataArray[parentIndex];
while(childIndex < Size)
{
if (childIndex < Size-1) //下标未越界
{
if (DataArray[childIndex] < DataArray[childIndex+1]) //选取左右孩子的大者
{
childIndex++;
}
}
if (parentValue < DataArray[childIndex]) //当前节点小于孩子
{
DataArray[pare