Data Structures & AlgorithmsHardGATE CS · Infosys
The array [19, 25, 3, 11, 7, 15, 4, 18, 13, 24, 23] (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. 5
- B. 23
- C. 6
- D. 4
Correct answer: D. 4
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 [16, 15, 9, 6, 4, 5, 1] (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 array [7, 29, 10, 23, 27, 22, 21, 2, 16, 13] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1…
- The array [5, 11, 13, 2, 23, 26, 24, 18] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to…
- The array [29, 16, 15, 27, 4, 6, 18, 25, 3, 8, 5, 7] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2…
- The array [12, 5, 1, 7, 4, 28, 20] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to 0…
- The array [28, 22, 18, 8, 2, 19, 9] (0-based indices) is turned into a binary min-heap by Floyd's method: for i from n/2 − 1 down to 0…