Heapify in 15 Seconds – Visual Max-Heap Construction

Опубликовано: 12 Июль 2026
на канале: Andrey Z.
841
11

Heapify (max-heap construction)

1. Treat the entire array as a complete binary tree.
2. Start at the last internal node ⌊n/2⌋-1 and move upward to the root.
3. On each node perform sift-down: compare with its children, swap with the larger child until the node is ≥ both children.
4. After all internal nodes are processed, the tree is a max-heap: the root holds the largest value and every parent ≥ its children.

Time complexity: O(n) Space: O(1).