GATE Computer ScienceModerateGATE CS (PYQ pattern)
The keys 3, 9, 2, 1, 4, 5, 7, 8, 6, 10, 11, 12 are inserted, in this order, one at a time into an initially empty binary max-heap, each insertion sifting the new key up. How many exchanges are performed in total? (Floyd's bottom-up Build-Max-Heap on the same array would perform only 9.)
- A. 9
- B. 16
- C. 14
- D. 19
Correct answer: B. 16
Sift-up exchanges per insertion: 3:0, 9:1, 2:0, 1:0, 4:1, 5:1, 7:1, 8:2, 6:1, 10:3, 11:3, 12:3 — total 16; the final heap is [12, 10, 11, 6, 9, 7, 5, 1, 4, 3, 8, 2]. Top-down insertion is O(n log n) in the worst case and here costs more than Floyd's 9.
More Algorithms questions
- The array [5, 3, 17, 10, 84, 19, 6, 22, 9, 15, 2] is turned into a max-heap by Floyd's linear-time Build-Max-Heap. Which array results?…
- Insertion sort on [7, 3, 9, 1, 8, 2, 6, 5, 4] scans left-to-right from position j = i − 1, comparing a[j] with the key until a[j] ≤ key or…
- Lomuto partition (pivot = last element, loop j from lo to hi−1 comparing a[j] ≤ pivot) is used in quicksort on [5, 2, 8, 1, 9, 3, 7, 4…
- A randomised quicksort on a random permutation of n distinct keys picks a pivot uniformly and every partition of m elements costs exactly…
- Binary search on a sorted array of 20 distinct keys uses mid = ⌊(lo + hi)/2⌋ and one three-way comparison per probe, stopping when lo >…
- A base-3 (ternary) counter starts at 0 and is incremented 1000 times. The cost of an increment is the number of digits that change…