Priority queues
A queue where the most urgent item leaves first, whatever order things arrived in. Seven steps: the contract, two slow ways to build it, and the heap that makes it fast.
In 30 seconds
A priority queue holds items that each carry a priority. You can add an item, and you can take out the most urgent one. Arrival order does not matter.
That description is only a contract. A plain array can honour it, but either adding or removing then takes a step per item.
The usual implementation is a heap, a loosely ordered tree that keeps the most urgent item on top and does both operations in about log n steps. Shortest-path search, schedulers and simulations all rely on it.
Priority queues, step by step
Shown as a min-queue: the smallest number is the most urgent. A max-queue is the same idea flipped. Real heaps store the tree in a plain array; the Heaps lesson shows how.
- Step 1: Most urgent first. An ordinary queue serves whoever arrived first. A priority queue ignores arrival order: every item carries a priority, and the most urgent one leaves first. Here a smaller number means more urgent, so 1 jumps ahead of everything that came before it.
- Step 2: The contract: two operations. A priority queue is an abstract data type: a promise about behaviour. You can insert an item with its priority, and you can extract the item with the smallest priority. Peek shows that item without removing it. Nothing in the promise says how the items are stored inside.
- Step 3: Two simple builds, both slow. Keep the items in an unsorted array and inserting is instant, but finding the smallest means checking every cell. Keep the array sorted and the smallest is ready at the end, but each insert must shift items to make room. With n items, one of the two operations always costs about n steps.
- Step 4: A heap balances both. The usual implementation is a binary heap: a tree, filled level by level, in which every parent is smaller than or equal to its children. That one rule puts the smallest item at the top, while leaving the rest only loosely ordered. Loose order is cheap to keep, so both operations take about log n steps.
- Step 5: Insert: bubble up. To insert 2, the heap puts it in the next free slot at the bottom, then compares it with its parent. 2 is smaller than 8, so they swap. One level up, the parent is 1, which is smaller, so 2 stops. An item climbs at most one level per swap, and a heap of n items has about log n levels.
- Step 6: Extract-min: sift down. Removing the smallest leaves a hole at the top. The heap moves its last item, 8, into the hole, then repairs the order: 8 is compared with its children 3 and 2 and swaps with the smaller one. Now 2 is on top and every parent is again no bigger than its children. The repair walks down one level per swap.
- Step 7: Where they are used. Many programs repeat one question: which item is next? Dijkstra’s shortest-path algorithm keeps asking for the unvisited node with the smallest distance. A scheduler picks the waiting task with the highest priority. An event simulation keeps future events in a priority queue and always processes the earliest.
Try it yourself
Learn this first
Learn next
Spotted a mistake on this page?