Data Structures & AlgorithmsModerateGATE CS · Infosys
The array [16, 9, 13, 29, 7, 18, 6, 28, 14, 27, 20, 17] (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?
- A. 7
- B. 28
- C. 5
- D. 6
Correct answer: D. 6
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 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…
- The array [19, 3, 25, 10, 1, 22, 21, 20, 7, 4, 15, 28] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from…
- The array [18, 5, 16, 1, 24, 14, 9, 15, 28, 7, 2, 21] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2…
- The array [11, 20, 14, 23, 10, 28, 19, 27] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down…
- The array [5, 7, 25, 10, 3, 9, 29, 19, 27, 17, 21] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 −…
- The array [17, 12, 25, 28, 9, 16] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to 0, sift…