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.)

  1. A. 9
  2. B. 16
  3. C. 14
  4. 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

Exams that ask this topic