Heapify rearranges a heap’s array representation to restore the min-heap or max-heap property after an update or during construction. section 1.2
Heapify has two common directions:
- Heapify up compares a newly inserted element with its parent and swaps them when the heap property is violated. section 1.2
- Heapify down compares a violating element with its smallest child in a min-heap, or largest child in a max-heap, and swaps as needed. section 1.2
- Building a heap from an unsorted array uses heapify down and costs O(n), rather than inserting each element individually at O(n log n). section 1.2
Repairing after insertion
When an element is inserted, compare it with its parent. If it violates the min-heap or max-heap property, swap upward and continue until the property is restored. section 1.2


