Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning data structure & algorithm. I want to understand how to build max-heap in O(n) time complexity. I have read several articles, still it is not clicking in my mind. I am looking for something in plain english & less technicality. Please share any useful resource you have. Or if you have time, plz explain here.

What I have tried:

Read several articles, but still it looks to me as O(n log n).
Posted
Updated 21-Aug-22 6:50am

O(n) time binary heap building is possible using bottom-up approach. But heap elements should be predefined & fixed. If new elements keep coming, then bottom-up approach won't work with same complexity. You can try StackOverFlow site. It contains similar questions & answers I believe. Or you can also try this article Building Binary Heap In O(n) Worst Time Complexity Explained – W3 Spot[^] . It compares both top-down & bottom-up approaches with time complexity analysis. There are diagrams & examples in the post which should be simple enough to understand.
 
Share this answer
 
Quote:
Can someone plz explain me max-heap building in O(n) time?

Extremely simple, you don't. It does not exist.
Technically, it is O(n)=n, and this means that you need a fixed amount of work/time to get the final position in the heap.
For O(n)=n log n, this means that the amount of work/time needed to get the final position depends on the amount of data already in heap.

[Update]
In fact, it is possible for the very restricted conditions as explained in solution 3.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900