21,695 views
41 votes
41 votes

We have a binary heap on $n$ elements and wish to insert $n$ more elements (not necessarily one after another) into this heap. The total time required for this is

  1. $\Theta(\log n)$

  2. $\Theta(n)$

  3. $\Theta(n\log n)$

  4. $\Theta(n^2)$

7 Answers

3 votes
3 votes

The worst case time complexity for insertion in a binary heap is O(Logn) (Refer Wiki). So inserting n elements in a heap of size n should take Θ(nlogn) time.
But choice (B) seems to be more appropriate answer. One of the solution of O(n) complexity can be to take the ‘n’ elements of the heap and other ‘n’ elements together and construct heap in O(2n) = O(n)

1 votes
1 votes

Answer is (B). 

We can imagine that these first 'n' elements are also not a heap. And then simply add these next 'n' elements at the end of the array. And consder total 2n elements in an array and then call Build Heap(). We know that build heap takes O(n) times. So for these 2n elements it will also takes O(n) times. So is the answer.

Answer:

Related questions

79 votes
79 votes
7 answers
3
Kathleen asked Sep 12, 2014
36,609 views
The minimum number of comparisons required to determine if an integer appears more than $\frac{n}{2}$ times in a sorted array of $n$ integers is$\Theta(n)$$\Theta(\log n)...
28 votes
28 votes
6 answers
4
Kathleen asked Sep 11, 2014
13,414 views
The most efficient algorithm for finding the number of connected components in an undirected graph on $n$ vertices and $m$ edges has time complexity$\Theta(n)$$\Theta(m)$...