GATE Computer ScienceVery hardGATE CS
Twelve keys 24, 17, 31, 9, 22, 5, 38, 13, 2, 27, 6, 20 are used to build a min-heap in an array in two ways: (1) by inserting them one at a time with sift-up, and (2) by Floyd's bottom-up build-heap with sift-down. Counting each exchange of a parent with a child as one swap, what are the numbers of swaps for (1) and (2) respectively?
- A. (12, 9)
- B. (12, 10)
- C. (11, 9)
- D. (13, 8)
Correct answer: A. (12, 9)
Running both procedures gives 12 swaps for repeated insertion and 9 for Floyd's method (final heaps [2,5,9,13,6,20,38,24,17,27,22,31] and [2,6,5,9,22,20,38,13,17,27,24,31]). The counts differ because the two procedures build different heaps.
More Algorithms questions
- Which is the tight asymptotic solution of T(n) = T(n/3) + T(2n/3) + n/log₂ n (with T(1) = 1)?
- A flow network has directed edges (capacity): s→a 4, s→b 3, a→b 2, a→c 2, b→c 2, b→d 3, c→t 4, d→t 2, c→d 1. Let F be the maximum s–t flow…
- For the Petersen graph (10 vertices, 15 edges, 3-regular, independence number 4), let VC be the size of a minimum vertex cover and MM the…
- Binary insertion sort sorts 10 distinct elements. It inserts the k-th element into the sorted prefix of k − 1 elements by binary search…
- A sorted array holds 1000 distinct elements and binary search (one three-way comparison per probe) is applied. What is the worst-case…
- What is the asymptotic solution of T(n) = 3T(n/2) + n² with T(1) = 1?