https://www.jianshu.com/p/938789fde325
//
// main.c
//
// Created by 柏超曾 on 3/20/18.
// Copyright © 2018 柏超曾. All rights reserved.
//
#include <stdio.h>
//给定父节点的索引,得到左子节点的索引
#define LeftChild(i) (2*(i)+1)
//元素下沉方法
void PercDown(int A[], int i, int N)
{
//子节点的索引号
int child;
//存储当前父节点元素的临时变量
//(注:每一个节点都可以看作是其子树的根节点)
int tmp;
for (tmp = A[i]; LeftChild(i)<N; i = child)
{
child = LeftChild(i);
//挑选出左、右子节点中较大者
if (child != N-1 && A[child+1]>A[child])
{
child++;
}
//比较当前父节点与较大子节点
if (A[i]<A[child])
{
//交换当前父节点处的元素值与较大子节点的元素值
tmp= A[i];
A[i] = A[child];
A[child] = tmp;
}
else
{
break;
}
}
}
//交换两个元素的位置
void Swap(int *num1, int * num2)
{
int tmp = *num1;
*num1 = *num2;
*num2 = tmp;
}
//堆排序
void HeapSort(int A[], int N)
{
int i;
//步骤一:创建大根堆
for (i = N/2; i>=0; i--)
{
PercDown(A, i, N);
}
//步骤二:调整大根堆
for ( i = N-1; i > 0; i--)
{
//首尾交换
Swap(&A[0], &A[i]);
//元素下沉
PercDown(A, 0, i);
}
}
//主函数
int main(int argc, const char * argv[])
{
int A[6] = {4,5,3,2,6,1};
HeapSort(A, 6);
for(int i = 0; i < 6;i ++)
{
printf("%d ", A[i]);
}
return 0;
}