1.保持最大堆的性质:2.建立最大堆
3.堆排序
1 MAX_HEAPIPY(A,i)
2 l=left(i)
3 r=right(i)
4 if(l<heap-size(A) && a[l]>a[i])
5 then largest=l;
6 else largest=i
7 if(r<heap-size(A) && a[r]>a[largest])
8 then largest=r
9 if(largest != i)
10 then exchange A[i] <-> A[largest]
11 MAX_HEAPIFY(A,largest)
12
13 BUILD_MAX_HEAP(A)
14 head-size(A)=length(A)
15 for(i=length[A]>>1) downto 1
16 do MAX_HEAPIFY(A,i)
17
18 HEAP_SORT(A)
19 BUILD-MAX-HEAP(A)
20 for(i=length[A]) downto 2
21 do exchange A[1] <-> A[i]
22 heap_size[A]=heap_size(A)-1
23 MAX_HEAPIFY(A,1)