Practice mode Data Structures

The array is turned into a max-heap using the bottom-up build-heap method, sifting down from the last internal node to the root. What is the resulting array?

Correct answer C. [9, 4, 5, 1, 3, 2]

Explanation

Start at index 2 (value 2): its child 5 is larger, so swap to get [3, 9, 5, 1, 4, 2]. Index 1 (value 9) is larger than 1 and 4, so it stays. At the root, 3 swaps with 9, then 3 swaps with its larger child 4, giving [9, 4, 5, 1, 3, 2]. The fully sorted descending array is a valid heap but not what the algorithm produces.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.