recategorized by
842 views
2 votes
2 votes
Consider the process of inserting an element into a max heap, where the max heap is represented by an array.Suppose we perform a binary search on the path from the new leaf to the root to find the position for newly inserted element, the number of comparisons performed is? # i m getting as O(logn) as upon applying binary search we wud compare O(logn) elements(neighbours and its own root only) plz clr my confusion
recategorized by

2 Answers

2 votes
2 votes

Yes, number of comparisons would be O(log n). But if you apply binary search, then there is no use of heap and actual insertion is much costlier, comparison O(log n) + O(n)shifting.

If you use heapify procedure you'll get O(log n) in total for insertion.

0 votes
0 votes
number of elements from  leaf to node =logn elements.

finding postion where node is to be inserted=loglogn(as for n elements binary search takes logn time).number of comparisons =loglogn.

now insert the element at position and call heapify(logn).  therfore total time loglogn+logn =O(logn).

Related questions

4 votes
4 votes
1 answer
1
Aghori asked Nov 5, 2017
1,212 views
There are two sorted list each of length $n$. An element to be searched in the both the lists. The lists are mutually exclusive. The maximum number of comparisons require...
0 votes
0 votes
2 answers
2
dhruba asked Jun 5, 2023
1,165 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). ...
3 votes
3 votes
2 answers
3
rahul sharma 5 asked Mar 8, 2018
2,408 views
Why to prefer binary search over ternary search?Can someone give recreance relation for ternary search,so that i can compare both