GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Heapsort (max-heap, in-place) is applied to [9, 4, 7, 1, 8, 2, 6, 3, 5, 10, 12, 11, 15, 14, 13]. First the array is made a max-heap with Floyd's bottom-up method; then the extract step (swap root with the last unsorted element, shrink the heap, sift down) is performed exactly 4 times. What is the array at that moment?

  1. A. 11 10 9 5 8 7 6 3 1 4 2 12 13 14 15
  2. B. 15 14 13 12 2 4 1 3 6 7 8 5 9 10 11
  3. C. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  4. D. 11 10 9 5 8 7 6 3 1 4 2 15 14 13 12

Correct answer: A. 11 10 9 5 8 7 6 3 1 4 2 12 13 14 15

Build-heap gives 15 12 14 5 10 11 13 3 1 4 8 7 2 6 9. Each extraction places the current maximum at the end: after 4 steps the last four cells hold 12, 13, 14, 15 in ascending order and the first 11 cells are a max-heap.

More Programming and Data Structures (GATE) questions

Exams that ask this topic