GATE Computer ScienceHardGATE CS (PYQ pattern)

The keys 20, 15, 30, 10, 5, 25, 3, 18, 1 are inserted one by one into an initially empty min-heap (array form, each insert sifts up). How many swaps occur in total, and which key is at array index 4 (0-indexed) at the end?

  1. A. 13 swaps, key 15
  2. B. 12 swaps, key 20
  3. C. 12 swaps, key 15
  4. D. 6 swaps, key 15

Correct answer: C. 12 swaps, key 15

Sift-up after each insertion: the swap counts are accumulated step by step to 12. The final heap array is [1, 3, 5, 10, 15, 30, 25, 20, 18]; building bottom-up instead would give a different array.

More Programming and Data Structures (GATE) questions

Exams that ask this topic