~/ learn/ comp-372/ cards/ Sorting & Selection: Better, Faster, and the Limits
1 of 34

Type MAX-HEAPIFY (CLRS, 1-indexed pseudocode)

Type MAX-HEAPIFY (CLRS, 1-indexed pseudocode)

Answer

MAX-HEAPIFY(A, i) l = LEFT(i); r = RIGHT(i) if l <= A.heap-size and A[l] > A[i] largest = l else largest = i if r <= A.heap-size and A[r] > A[largest] largest = r if largest != i exchange A[i] with A[largest] MAX-HEAPIFY(A, largest)

It picks the largest of node i and its two children; if a child wins, it swaps and recurses into that child. The misplaced value sinks at most lg n levels, giving O(lg n).

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/50b0ebf0-80e0-444e-9070-6a0a1607ea20/flashcard utf-8 LF