On this page
A priority queue answers one question quickly: which item is most urgent right now? MIT's 6.006 lecture notes on binary heaps list routers prioritising messages, operating systems scheduling processes, event simulations and graph algorithms as places that need one. A binary heap does the job, and you can trace it by hand.
What is a binary heap?
In the words of the Python heapq documentation, "Min-heaps are binary trees for which every parent node has a value less than or equal to any of its children." That rule is called the heap invariant, and it puts the smallest element at the root.
The tree is complete: every level is full except the last, which fills from the left. So you can store it in a plain array with no pointers. Counting from 0, the children of index i sit at 2i + 1 and 2i + 2, and its parent at floor((i − 1)/2), as the MIT 6.006 notes set out.
Our heap of 10 numbers is [2, 5, 3, 8, 9, 6, 4, 12, 10, 11]. Index 1 holds 5, whose children are 8 (index 3) and 9 (index 4). The array is not sorted: a heap only promises order along each path from root to leaf.
How do you insert into a heap?
Insertion uses sift-up. Put the new item in the next free slot, then swap it with its parent while it is smaller. Let us insert 1.
Place 1 at index 10. Its parent is index 4, holding 9. Since 1 < 9, swap.
Now 1 is at index 4. Its parent is index 1, holding 5. Since 1 < 5, swap.
Now 1 is at index 1. Its parent is index 0, holding 2. Since 1 < 2, swap. It is at the root, so stop.
The array is now [1, 2, 3, 8, 5, 6, 4, 12, 10, 11, 9]: three swaps, one per level, the most an 11-item heap can need.
How do you remove the minimum from a heap?
Removal uses sift-down. Take the root, move the last item there, then swap it with its smaller child while it is bigger.
Remove 1. Move 9 from index 10 to the root: [9, 2, 3, 8, 5, 6, 4, 12, 10, 11].
The root's children are 2 and 3. The smaller is 2, and 9 > 2, so swap: 9 moves to index 1.
Index 1's children are 8 and 5. The smaller is 5, and 9 > 5, so swap: 9 moves to index 4.
Index 4's only child is 11. Since 9 < 11, stop.
You are back at the heap you started with, a handy self-check.
Why are heap operations O(log n)?
Each swap moves an item one level, and a complete tree with n items is only about log₂ n levels tall. The MIT notes give both insert and delete-max a running time of Θ(log n). A heap of a million items has 20 levels, so the worst insert makes 19 swaps rather than scanning a million entries. Our guide to Big O notation and growth rates explains what Θ and O promise.
How do you build a heap in O(n)?
Inserting n items one by one costs O(n log n) in the worst case. The faster method runs sift-down on each node from the last index back to the root. Most nodes sit near the leaves with little room to fall, and the MIT notes sum the worst-case swaps to show the total is O(n).
Our 10 positions show why. Sift-up's worst case is each node's depth: 0 + 1 + 1 + 2 + 2 + 2 + 2 + 3 + 3 + 3 = 19 swaps. Sift-down's worst case is each node's height: 3 + 2 + 1 + 1 + 1 + 0 + 0 + 0 + 0 + 0 = 8 swaps. Python's heapify, says its documentation, likewise works "in-place, in linear time".
How does heapsort work?
J. W. J. Williams published Heapsort (Algorithm 232) in Communications of the ACM in June 1964 (doi:10.1145/512274.3734138); Robert W. Floyd's Treesort (Algorithm 245) followed there that December (doi:10.1145/355588.365103). In three steps:
Build a max-heap, so the largest item is at index 0.
Swap the root with the last item and shrink the heap by one; the largest item is now in its final place.
Sift the new root down and repeat until the heap is empty.
The MIT notes show this sorts in place in O(n log n); the O(n) build does not lower that, because the n removals still cost O(log n) each.
Test yourself in five minutes:
Write the parent and children of index 7 in a 0-indexed heap.
Insert 7 into our heap of 10 and count the swaps.
Remove the minimum twice and write each array.
Say in one sentence why a heap is not a sorted array.
If sift-down still feels shaky, the Fermi Concept Library covers every concept in mathematics and computing with its own lesson, and the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.
For more algorithms worked by hand, see Bellman–Ford shortest paths with negative edges and topological sort on a DAG.



