ProgrammingHardGATE CS & IT · Infosys

What is the time complexity of building a binary heap from n unordered items with heapq.heapify?

  1. A. O(n)
  2. B. O(n log n)
  3. C. O(log n)
  4. D. O(n^2)

Correct answer: A. O(n)

Bottom-up heapify sifts down each node by an amount that depends on its height, and the total works out to O(n). Pushing the items one by one with heappush would cost O(n log n).

More Python Programming questions

Exams that ask this topic