Open In App

Algorithms | Sorting | Question 9

Like Article
Like
Save Article
Save
Share
Report issue
Report

Suppose we are sorting an array of eight integers using heapsort, and we have just finished some heapify (either maxheapify or minheapify) operations. The array now looks like this: 16 14 15 10 12 27 28 How many heapify operations have been performed on root of heap?

(A)

1

(B)

2

(C)

3 or 4

(D)

5 or 6



Answer: (B)

Explanation:

In Heapsort, we first build a heap, then we do following operations till the heap size becomes 1. a) Swap the root with last element b) Call heapify for root c) reduce the heap size by 1. In this question, it is given that heapify has been called few times and we see that last two elements in given array are the 2 maximum elements in array. So situation is clear, it is maxheapify which has been called 2 times.

Hence Option(B) is the correct answer.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 22 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads