Data Structures & AlgorithmsVery hardGATE CS · Infosys

The array [6, 28, 24, 5, 29, 20, 27, 3, 2, 23, 7, 13] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to 0, sift the element at i down by repeatedly swapping it with its smaller child while that child is smaller. How many swaps are performed in total?

  1. A. 10
  2. B. 6
  3. C. 5
  4. D. 40

Correct answer: A. 10

Floyd's build-heap sifts from the last internal node back to the root. Each swap is counted once; building a max-heap, or sifting from the top down, performs a different number of swaps.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic