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
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.
More Programming, Data Structures, and Algorithms questions
- For Quicksort using deterministic last-element pivoting on an array of n distinct elements that is already sorted in…
- Consider the recurrence relation T(n) = 4T(n / 2) + n^2 for n > 1, with T(1) = 1. Using the Master Theorem, what is…
- What is the length of the Longest Common Subsequence (LCS) of the two strings X = 'ABCBDAB' and Y = 'BDCABA'?
- A connected undirected weighted graph G has 8 vertices and 15 edges with distinct positive edge weights. How many…
- Which two statements accurately compare Dijkstra's single-source shortest-path algorithm and the Bellman-Ford…
- A message uses five characters A, B, C, D, and E with frequencies 5, 9, 12, 13, and 16 respectively. Using optimal…
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.