Data Structures & AlgorithmsModerateGATE CS

Heap sort (ascending) is run on [3, 13, 29, 24, 7, 25, 19, 40]: build a max-heap bottom-up, then repeatedly swap the root with the last element of the heap part and sift down within the shrunk heap. What is the array after the build phase and exactly 2 extractions?

  1. A. [29, 24, 25, 13, 7, 3, 19, 40]
  2. B. [25, 24, 19, 13, 7, 3, 29, 40]
  3. C. [24, 13, 19, 3, 7, 25, 29, 40]
  4. D. [40, 24, 29, 13, 7, 25, 19, 3]

Correct answer: B. [25, 24, 19, 13, 7, 3, 29, 40]

Build-heap gives [40, 24, 29, 13, 7, 25, 19, 3]; each extraction parks the current maximum at the end of the array and re-heapifies the prefix, giving [25, 24, 19, 13, 7, 3, 29, 40] after 2 extractions.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic