Data Structures & AlgorithmsModerateProduct-company placement

Heap sort (ascending) is run on [20, 37, 49, 16, 22, 10, 31, 33]: 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. [33, 22, 31, 16, 20, 10, 37, 49]
  2. B. [37, 33, 31, 16, 22, 10, 20, 49]
  3. C. [31, 22, 10, 16, 20, 33, 37, 49]
  4. D. [49, 37, 31, 33, 22, 10, 20, 16]

Correct answer: A. [33, 22, 31, 16, 20, 10, 37, 49]

Build-heap gives [49, 37, 31, 33, 22, 10, 20, 16]; each extraction parks the current maximum at the end of the array and re-heapifies the prefix, giving [33, 22, 31, 16, 20, 10, 37, 49] after 2 extractions.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic