Open In App

Data Structures | Heap | Question 1

Like Article
Like
Save Article
Save
Share
Report issue
Report

What is the time complexity of Build Heap operation? 
(Build Heap is used to build a max(or min) binary heap from a given array. Build Heap is used in Heap Sort as a first step for sorting.)

(A)

O(nLogn)

(B)

O(n^2)

(C)

O(n)

(D)

O(logn)



Answer: (C)

Explanation:

Following is algorithm for building a Heap of an input array A.

BUILD-HEAP(A) 
    heapsize := size(A); 
    for i := floor(heapsize/2) downto 1 
        do HEAPIFY(A, i); 
    end for 
END

Although the worst-case complexity looks like O(N log N), upper bound of time complexity is O(n). See following links for the proof of time complexity. Time Complexity of building a heap.

Hence Option(C) is the correct answer.


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


Last Updated : 07 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads