Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional

Array [27, 21, 1, 3, 16, 28, 17] 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. [1, 3, 17, 21, 16, 28, 27]
  2. B. [1, 3, 16, 17, 21, 27, 28]
  3. C. [1, 21, 17, 3, 16, 28, 27]
  4. D. [1, 3, 17, 27, 16, 28, 21]

Correct answer: A. [1, 3, 17, 21, 16, 28, 27]

Bottom-up heapify on [27, 21, 1, 3, 16, 28, 17] produces [1, 3, 17, 21, 16, 28, 27]. 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