MAX_HEAPIFY(A,i)
- l ← LEFT(i)
- r ← RIGHT(i)
- if l ≤ heapSize and A[l] > A[i] then
- largest ← l
- else
- largest ← i
- if r ≤ heapSize and A[r] > A[largest] then
- largest ← r
- if largest ≠ i then
- exchange(A[i],A[largest)
- MAX_HEAPIFY(A, largest)
BUILD_MAX_HEAP(A)
- heap-size ← n
- for i ← n/2 down to 1 do
- MAX_HEAPIFY(A,i)
HEAP_SORT(A)
- heap-size = n
- BUILD_MAX_HEAP(A)
- fori← n down to 2
- exchange(A[i],A[1]) // move the biggest to the end of the array
- heap-size ← heap-size - 1
- MAX_HEAPIFY(A,1)