Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2

Array [28, 17, 3, 22, 8, 23, 10] 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. [3, 8, 10, 28, 22, 23, 17]
  2. B. [3, 17, 10, 22, 8, 23, 28]
  3. C. [3, 8, 10, 17, 22, 23, 28]
  4. D. [3, 8, 10, 22, 17, 23, 28]

Correct answer: D. [3, 8, 10, 22, 17, 23, 28]

Bottom-up heapify on [28, 17, 3, 22, 8, 23, 10] produces [3, 8, 10, 22, 17, 23, 28]. 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