GATE Computer ScienceHardGATE CS (PYQ pattern)

The array [4, 10, 3, 5, 1, 8, 7, 2, 9, 6, 11, 12] is converted into a max-heap by Floyd's bottom-up build (sift-down from the last non-leaf to the root). What is the total number of swaps and the final value at index 5 (0-indexed)?

  1. A. 8 swaps, 4
  2. B. 10 swaps, 4
  3. C. 8 swaps, 10
  4. D. 6 swaps, 7

Correct answer: A. 8 swaps, 4

Sift down nodes at indices 5 down to 0, swapping with the larger child each time; the swaps total 8 and the final heap is [12, 11, 8, 9, 10, 4, 7, 2, 5, 6, 1, 3].

More Programming and Data Structures (GATE) questions

Exams that ask this topic