How can building a heap be O(n) time complexity?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
At first glance, heap construction looks like it should cost O(n log n) because inserting one element into a heap costs O(log n), and doing that n times suggests n log n. But that reasoning applies to repeated insertion, not to Floyd's bottom-up heap construction algorithm. The bottom-up method is O(n) because most nodes are near the leaves, and nodes near the leaves are cheap to heapify.
The Expensive Intuition Is Incomplete
If you build a heap this way:
- start with an empty heap
- insert each element one by one
then yes, the total cost is typically O(n log n).
But there is another method: treat the whole array as a nearly complete binary tree and call heapify from the last internal node upward.
That is the linear-time algorithm people mean when they say heap construction is O(n).
Why Bottom-Up Heapify Is Cheaper
Heapify on one node can cost up to the height of that node, not the height of the entire tree. Nodes at the bottom have height 0, nodes just above them have height 1, and only a tiny number of nodes are near the root with large height.
So the total work is not:
It is closer to:
That sum turns out to be linear in n for a complete binary tree.
The Key Counting Argument
In a complete binary tree:
- about
n/2nodes are leaves, so their heapify cost is0 - about
n/4nodes have height1 - about
n/8nodes have height2 - and so on
So the total work is bounded by:
Factor out n:
The infinite series in parentheses converges to a constant, so the whole expression is O(n).
That is the real reason. The average node is cheap to fix because most nodes live low in the tree.
A Concrete Example
Suppose you have this array:
You can build a min-heap bottom-up like this:
This is a real O(n) heap build, not a sequence of O(log n) insertions.
Where The Confusion Comes From
People often mix up two different operations:
- building a heap from an existing array with bottom-up heapify:
O(n) - inserting
nitems one at a time into an initially empty heap:O(n log n)
Both statements are true, but they describe different algorithms.
Once you distinguish those cases, the contradiction disappears.
Intuition In One Sentence
The root is expensive to heapify, but there is only one root. The leaves are cheap to heapify, and there are many leaves. The cheap nodes dominate the count.
That is why the total is linear even though a single heapify can be logarithmic in the worst case.
Common Pitfalls
- Assuming the cost of building a heap must equal the cost of inserting elements one by one.
- Thinking every node pays the full
log nheapify cost during bottom-up construction. - Forgetting that leaves and near-leaf nodes make up most of the tree.
- Proving the result informally with intuition but not separating the two heap-building algorithms.
- Using "heapify" and "insert" interchangeably even though they are different operations with different total costs.
Summary
- Building a heap by repeated insertion is
O(n log n). - Building a heap bottom-up from an array is
O(n). - The bottom-up cost is linear because most nodes are near the leaves and have tiny heapify cost.
- The total work is the sum of node heights, not
ncopies oflog n. - The apparent contradiction disappears once you distinguish the two construction methods.
Related reading
- How can building a heap be On time complexity?
- How can I adapt the Levenshtein Distance algorithm to limit matches to a single word?
- How can I algorithmically determine optimal block placement in a Block Blast-style puzzle solver?
- How can I analyze or improve my niece's simple compression algorithm that is based on Morse code?
- How can I access and process nested objects, arrays, or JSON?
- How can I access the filenames gathered by tf.data.Dataset.list_files?
- How can get data from address-book faster in Android?
- How can Google be so fast?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.