Data Structures & AlgorithmsHardGATE CS · ICPC regional

Array [27, 5, 12, 25, 10, 1, 22, 2] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at index i down (swap with its smaller child while a child is smaller). What is the resulting array?

  1. A. [5, 10, 12, 25, 27, 1, 22, 2]
  2. B. [1, 2, 12, 5, 10, 27, 22, 25]
  3. C. [1, 2, 5, 10, 12, 22, 25, 27]
  4. D. [1, 2, 5, 10, 25, 12, 22, 27]

Correct answer: B. [1, 2, 12, 5, 10, 27, 22, 25]

Bottom-up heapify on [27, 5, 12, 25, 10, 1, 22, 2] produces [1, 2, 12, 5, 10, 27, 22, 25]. Inserting one element at a time gives a different heap, and a single sift-down from the root does not fix the internal nodes.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic