max-heapify (A, i) left =left (i) right=right (i) if (left<=heap-size[ A] and A[left]>a[i]) then largest=left Else largest=i if (right <=heap-size[A] and A[right]>a[largest] then Largest=right if (largest!=i) then Exchange (A[i], a[largest]) max-heapify (A, largest)
Buld-max-heap (a) heap-size[a]=legnth (a) for I=floor (length (a)/2) Downto 1 do max_heapify (A, i)
Heap-sort (a) build-max-heap (a) for I=length (a) Downto 2 do Exchange (A[1], a[i]) heap-size (a) = Heap-size[a]-1 max-heapify (A, 1)
Application:
1. For example, given the number of n (n very Large), the largest k, you can read the number of K, the minimum heap, and then with the remaining number of n-k to compare the heap top elements, if the number I is less than the top element of the heap, then the next round, if the number I is greater than the top element of the heap, Then tidy the minimum heap (max-heapifiy (A, 1)).
Algorithm: Heap Ordering