52 52 votes An array $X$ of $n$ distinct integers is interpreted as a complete binary tree. The index of the first element of the array is $0$. If only the root node does not satisfy the heap property, the algorithm to convert the complete binary tree into a heap has the best asymptotic time complexity of $O (n)$ $O (\log n)$ $O (n \log n)$ $O (n \log \log n)$ Data Structures gateit-2006 data-structures binary-heap easy + – Ishrat Jahan 14.2k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply rishu_darkshadow commented Nov 16, 2017 reply Follow flag what will be the time complexity if worst case asked in question.. ?? 0 0 replyShare Sohail1432 commented Jan 27, 2018 reply Follow flag If they Ask Worst Case ,so what will be the Time complexity in Worst Case..Anyone Plz Clear my Doubt.. 1 1 replyShare vupadhayayx86 commented Oct 3, 2018 reply Follow flag Can I say this question is similar to https://gateoverflow.in/8091/gate2015-2-17 ?? One is asking lower bound other is asking best case!! 1 1 replyShare chauhansunil20th commented Nov 27, 2018 reply Follow flag Answer is $\Omega (1)$ and $O(logn)$ Hence, (B) is the correct option. 1 1 replyShare Mayank_Pant commented May 29 reply Follow flag rare event when year 2006 question is that straight forward 0 0 replyShare Please log in or register to add a comment.
Best answer 51 51 votes Here we need to call Heapify/ Bubble down/ Percolate down procedure on Root, which in the worst case will take time $O (\log n).$ So $B$ is the correct option. Other options do not even make sense, because with $O(n)$ we can even build an entire Heap not just heapify on the root. $O (n \log n) \;\&\; O (n \log \log n)$ is more than $O(n)$. Akash Kanase answered Nov 23, 2015 • edited Feb 1, 2023 by Abhrajyoti00 Akash Kanase comment Share Follow See all 4 Comments 4 4 Comments reply Himanshu1 commented Nov 25, 2015 reply Follow flag But what if O(1) would hv been in options ..?? 2 2 replyShare ShouvikSVK commented Dec 24, 2021 reply Follow flag just think root contains the smallest element among all elements in the heap Then We have to go either the entire Left Part or the entire Right Part of the whole Tree. 2 2 replyShare cormen commented Sep 23, 2024 reply Follow flag @Himanshu1, Omega(1) would've been right not O(1). 3 3 replyShare K_GANESH commented Jun 5 reply Follow flag they ask best asymptotic time complexity ..but options given in Upper bound ...so what we understand from that is...they ask what is upper bound in Best case..i.e we cant go further than in best case.... so 0(1) not be the answer if they mesion in option ..because it tells we can do at constant time in any best case ..but it is false be cause root can be go till last level also possible .. if they give omega(1)..it is correct because in some case we root just go to 1 level down .. 0 0 replyShare Please log in or register to add a comment.
27 27 votes The question is saying best case which will be when only one swap will be required which will be order of $1$. As no option matches just call heapify at the root - $O(\log n)$. Tendua answered Sep 5, 2015 • edited Jan 2, 2018 by kenzou Tendua comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments lakshaysaini2013 commented Oct 30, 2018 reply Follow flag @Ayush sir what wiil be the TC in case for worst asymptotic time complexity 0 0 replyShare Ayush Upadhyaya commented Oct 30, 2018 reply Follow flag @Lakshaysaini2013-Worst would be $O(n)$. Consider the the right subtree of root to be a min heap and left subtree to be a max heap. And you finally want a min heap. 0 0 replyShare Nirajanandan samal commented May 20 reply Follow flag most valuable answer❤️ 0 0 replyShare Please log in or register to add a comment.
7 7 votes Step 1:delete the root and replace it with last element and heapify takes O(log n) Step 2: inserting the deleted element again takes O(log n) time so its O(2log n) and finally O(log n) Bhagirathi answered Nov 3, 2014 Bhagirathi comment Share Follow See all 4 Comments 4 4 Comments reply Arjun commented Nov 4, 2014 reply Follow flag Why not simply call Heapify without an insert or delete? 12 12 replyShare Sandeep_Uniyal commented Jan 19, 2015 reply Follow flag @Arjun: You are correct ,why would we delete the element . We can just call heapify and it will also take logn time only as other nodes are already in place. 3 3 replyShare rishu_darkshadow commented Nov 16, 2017 reply Follow flag what will be the time complexity if worst case asked in question.. ?? 1 1 replyShare sardendu commented Sep 11, 2018 reply Follow flag Worst will be logn and i think the correct answer should be o(1) for the guven question as it is aksed the best time 0 0 replyShare Please log in or register to add a comment.