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?

  1. A. (12, 9)
  2. B. (12, 10)
  3. C. (11, 9)
  4. 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

Exams that ask this topic