recategorized by
663 views
0 votes
0 votes
If you are given a sorted list with n elements in ascending order. Then what will be the Time complexity to build a Min heap from the given array?
recategorized by

1 Answer

1 votes
1 votes

If its asked the minimum time, I would say time is $O(1)$ as the sorted array in ascending order satisfies the min-heap property

 

$array[i] <= array[2i]$ and $array[i] <=array[2i+1]$

If its asked time taken by build heap, it would be $O(N)$, as still the  loop runs from $floor(N/2)$ to $1$ irrespective of input:

BuildHeap(A) {
   heapsize <- length(A)
   for i <- floor( length/2 ) downto 1
      Heapify(A, i)
}

 

 

Related questions

0 votes
0 votes
1 answer
1
Akash Kumar Roy asked Apr 28, 2018
1,044 views
How much time will it take for deleting $i^{th}$ and a number $n(random)$ node from a heap?