GATE Computer Science · Programming, Data Structures, and Algorithms · easy

An initially empty array of 1,023 elements is converted into a binary min-heap in-place using Floyd's bottom-up build-heap algorithm, followed by one extract-min operation. What is the tight worst-case time complexity of the entire sequence for n elements?

Published · Last updated · checked before publication — how we verify questions

Options

  • ATheta(n) time because bottom-up heap construction is linear and extraction is O(log n)Correct answer
  • BTheta(n log n) time because elements sift up from each leaf to the root node
  • CTheta(log n) time because heapify visits nodes along a single root-to-leaf path
  • DTheta(n^2) time when the input array is initially sorted in descending order

Answer: A. Theta(n) time because bottom-up heap construction is linear and extraction is O(log n)

Floyd's build-heap calls sift-down on every internal node, starting from the last internal node and moving up to the root. A node at height h costs at most O(h) swaps, and a heap of n nodes has at most ceil(n / 2^(h+1)) nodes at height h. The total cost is the sum over h of n * h / 2^(h+1), and this sum converges to a constant times n, so the build is Theta(n). Most nodes sit near the leaves, where sift-down is cheap, so the O(log n) cost at the root is paid only once. One extract-min then moves the last element to the root and sifts it down, which costs O(log n). Since n + log n is Theta(n), the whole sequence is Theta(n). The common trap is to multiply n nodes by a height of log n and conclude Theta(n log n). That bound holds for building by n repeated insertions, not for the bottom-up method.

Why the other options are wrong

  • B. Theta(n log n) time because elements sift up from each leaf to the root node — Theta(n log n) is the cost of building a heap by n successive insertions, where each element sifts up and can travel O(log n) levels. Floyd's method sifts down from internal nodes, and most of those nodes have small height, so the total is linear.
  • C. Theta(log n) time because heapify visits nodes along a single root-to-leaf path — Theta(log n) is the cost of a single sift-down along one root-to-leaf path, which is what extract-min does. Building the heap runs sift-down on about n/2 internal nodes, and it also has to examine all n elements, so the sequence needs at least linear time.
  • D. Theta(n^2) time when the input array is initially sorted in descending order — Theta(n^2) is not reachable here, because the tree is complete with height floor(log n) and each sift-down is bounded by the node's height. A descending sorted input is a bad case for a min-heap, since many swaps happen, but the total work is still bounded by O(n) for any input order.

All Programming, Data Structures, and Algorithms questions and the topic overview →

GATE is a mark of the Indian Institute of Science and the Indian Institutes of Technology. PrepIQ Hub is not affiliated with, endorsed by, or sponsored by the GATE Committee.