Skip to content
Mad4Study

Engineering & Science

GATE Computer Science

  • GATE CS
  • 2026–27 pattern practice
  • Data Structures
  • Heaps
  • Medium

What is the worst-case time to build a binary heap from an unsorted array of nn elements using the bottom-up (heapify) method?

Trees

Pick an option to check your answer.

Bottom-up heap construction calls sift-down from the last internal node to the root. A node at height hh costs O(h)O(h), and there are at most ⌈n2h+1⌉\left\lceil \tfrac{n}{2^{h+1}} \right\rceil nodes at height hh:

∑h=0⌊log⁡n⌋n2h+1 O(h)=O ⁣(n∑h≥0h2h)=O(2n)=O(n)\sum_{h=0}^{\lfloor\log n\rfloor} \frac{n}{2^{h+1}}\,O(h) = O\!\left(n\sum_{h \ge 0}\frac{h}{2^h}\right) = O(2n) = O(n)

Why not the others

  • O(log⁡n)O(\log n) is the cost of a single insert or delete-min.
  • O(nlog⁡n)O(n\log n) is the cost of building by nn successive inserts — a valid but loose bound for heapify.
  • O(n2)O(n^2) has no basis for heap construction.

Report an error

GATE CS · Data Structures · Trees