| Build_Max_Heap (A) |
| 1 |
heap-size[A] ← length[A] |
| |
forall j; i<j≤length[A];
A[ j ] ≥
A[Left( j )] &&
A[ j ] ≥
A[Right( j )] |
| 2 |
for i ←
⌊length[A]/2⌋ downto 1
do |
| 3 |
Max_Heapify (A, i) |
|
Heapsort (A)
-- alters A
-- pre: location 1 of A satisfies the max heap
property
-- post: A is in non-decreasing sorted order |
| 1 |
Build_Max_Heap (A) |
| 2 |
for i ← length[A] downto
2 do |
| 3 |
exchange A[1] ↔ A[i] |
| 4 |
heap-size[A]
←
heap-size[A] - 1 |
| 5 |
Max_Heapify (A,
1) |
|