Heapsort

What Is the Time Complexity of Heap Operations?

A binary heap's peek is O(1), insert and removing the root are O(log n), building from n items is O(n), and searching is O(n). Here is why each holds.

On this page
  1. Short answer
  2. Operation costs for a binary heap
  3. Why most operations take logarithmic time
  4. Why building a heap takes linear time
  5. Why insertion is often fast
  6. Heapsort’s total cost
  7. Other heap types
  8. Reading the notation

Short answer

The time complexity depends on the operation. For a binary heap, peeking at the top is O(1), insert and removing the top are O(log n), building a heap from n items is O(n), and finding an arbitrary value is O(n). The table below sets out each one.

Operation costs for a binary heap

Operation Time Notes
Peek at the root (find the max or min) O(1) The root holds the extreme value
Insert O(log n) worst case Often close to O(1) on average for typical inputs
Remove the root O(log n) One sift-down path from the top
Build a heap from n items O(n) Floyd’s bottom-up method; inserting one at a time costs O(n log n)
Change an item’s priority O(log n) Needs the item’s position in the array, then one sift
Search for an arbitrary value O(n) Heap order says nothing about other branches
Space O(n) One slot per item

Why most operations take logarithmic time

A binary heap is a complete tree, so with n items its height is floor(log2 n). A heap of one million items is only about 20 levels deep, since 2^19 is 524,288 and 2^20 is 1,048,576. Removing the root moves one value from the top down along a single path. Inserting moves one value from the bottom up along a single path. Each path is at most the height of the tree, so each operation takes O(log n) steps in the worst case.

If the array layout is new to you, what is a heap data structure explains how the items are arranged.

Why building a heap takes linear time

Building a heap by inserting items one at a time costs O(n log n). The bottom-up method is faster. Half the nodes are leaves, and leaves need no work. Each level up has about half as many nodes, and their heapify paths can be one step longer. Adding up that pattern across all the levels gives a total that grows linearly with n.

Why insertion is often fast

A new item goes into the next free slot, at the bottom of the tree. It moves up only while it is larger than its parent. On typical inputs, most new items stop after a few levels, which is why the average cost is often close to constant. The worst case is still O(log n), when the new item must travel all the way to the root.

Heapsort’s total cost

Heapsort first builds a heap in O(n) time. It then removes the root n - 1 times, each at O(log n). The total is O(n) plus O(n log n), which simplifies to O(n log n). The full sequence of steps is covered in how the heapsort algorithm works.

Other heap types

Other heap designs trade simplicity for different bounds. A Fibonacci heap, for example, gives amortized O(1) time for decrease-key, which can help some shortest-path algorithms in theory. These structures are harder to implement and are rarely needed in everyday code. Array-based binary heaps remain the usual choice.

Reading the notation

The O notation used here describes how the cost grows with n. It does not give an exact running time on any particular machine. The notation is explained in the guide to big o data structures.

The complete guide to heapsort

How the Heapsort Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →

Recursive Algorithm Examples

Recursive algorithm examples in Python: factorial, binary search, tree traversal, Tower of Hanoi, and merge sort, with base cases explained for each.

Recursion 7 min read