1 void HeapSort(int H[],int length) {
2 BuildingHeap(H,length);
3 for (int i=length-1;i>0;--i) {
4 int temp=H[i];H[i]=H[0];H[0]=temp;
5 HeapAdjust(H,0,i);
6 }
7 }
8
9 void BuildingHeap(int H[],int length) {
10 for(int i=(length-1)/2;i>=0;i--)
11 HeapAdjust(H,i,length);
12 }
13
14 void HeapAdjust(int H[],int s,int length) {
15 int temp=H[s];
16 int child=2*s+1;
17 while(child<length) {
18 if(child+1<length && H[child]<H[child+1])
19 ++child;
20 if(H[s]<H[child]) {
21 H[s]=H[child];
22 s=child;
23 child=2*s+1;
24 }
25 else
26 break;
27 H[s]=temp;
28 }
29 }