Reference: http://www.codeproject.com/Tips/732196/Heap-Data-Structure-and-Heap-Sort#xx4775794xx
#include <cstdio>//The sift function://' sifts down ' the A[l] item until heap properties is restored//ARGS://a[] (INOUT) the array (where a[l+1], a[l+2]: a[size-1] is a partial heap)//size (in) number of items of a//l (in) index of the item inserted into the heapvoidSiftintA[],intSizeintl) { intI, J, X; I=l; J=2* i +1; X=A[i]; while(J <size) { //The maximum of left and right child if(J < Size-1) if(A[j] < A[j +1]) ++J; if(x >=A[j]) Break; A[i]=A[j]; I=J; J=2* i +1;//Sift} A[i]=x;}//makes the heap using the R.w.floyd ' s algorithm//ARGS://a[] (INOUT) the array wherein the heap is created//size (in) the size of the arrayvoidMake_heap (intA[],intsize) { intL = size/2; while(l) {--l; Sift (A, size, l); }}voidHeapsort (intA[],intsize) { intL =0, r =size; Make_heap (A, size); while(R >1) { intTMP = a[0]; --R; a[0] =A[r]; A[R]=tmp; Sift (A, R,0); }}intMain () {inta[Ten] = {1,3,2,5, One, $,7,9, +,0 }; Heapsort (A,Ten); for(inti =0; I <Ten; i++) {printf ("%d", A[i]); } return 0;}
Heap Data Structure and heap Sort