Priority Queues
A regular queue serves people in order of arrival. A priority queue serves the most urgent task next — regardless of when it arrived. It is the abstract data type that heaps were invented to implement.
What is a priority queue?
A priority queue always gives you the highest-priority (or lowest-value) item next, not the oldest. It's usually implemented with a binary heap, which makes insert and remove-top O(log n) and peeking O(1). Priority queues drive Dijkstra's shortest path, task schedulers, event simulations and "top K" problems.
// Minimal min-priority queue using a binary heap
class MinPQ {
h = [];
push(x) { this.h.push(x); let i = this.h.length - 1;
while (i && this.h[(i - 1) >> 1] > this.h[i]) { [this.h[i], this.h[(i - 1) >> 1]] = [this.h[(i - 1) >> 1], this.h[i]]; i = (i - 1) >> 1; } }
pop() { const top = this.h[0], last = this.h.pop();
if (this.h.length) { this.h[0] = last; let i = 0;
for (;;) { let s = i, l = 2 * i + 1, r = l + 1;
if (this.h[l] < this.h[s]) s = l; if (this.h[r] < this.h[s]) s = r;
if (s === i) break; [this.h[i], this.h[s]] = [this.h[s], this.h[i]]; i = s; } }
return top; }
}Visualizing a min-priority queue
Tasks inserted in random order — always extracted by lowest priority number first:
Live demo — min priority queue
Insert values with implicit priorities and watch the min-heap maintain the invariant. Extract-min always removes the smallest element in O(log n).
Min-Heap — parent ≤ children always
min = 1 · size = 6Priority queue interface
1PriorityQueue (min-heap implementation):23insert(value, priority):4 // Wrap value with its priority5 node <- { value, priority }6 heap.push(node) // append to heap7 siftUp(heap, heap.size - 1) // restore min-heap property8 // O(log n)910extractMin():11 if heap is empty: error12 min <- heap[0].value // root has smallest priority13 heap[0] <- heap[last]14 heap.removeLast()15 siftDown(heap, 0) // restore min-heap property16 return min17 // O(log n)1819peek():20 return heap[0].value // O(1)2122decreaseKey(node, newPriority): // used in Dijkstra23 node.priority <- newPriority24 siftUp(heap, node.index) // move up — priority got smaller25 // O(log n)
Implementations compared
| Implementation | insert | extractMin | peek |
|---|---|---|---|
| Unsorted array | O(1) | O(n) | O(n) |
| Sorted array | O(n) | O(1) | O(1) |
| ★ Binary heap | O(log n) | O(log n) | O(1) |
| ★ Fibonacci heap | O(1) amort. | O(log n) | O(1) |
Where priority queues show up
Extract the nearest unvisited vertex at each step. A min-heap makes each extraction O(log n).
Always expand the node with the lowest estimated total cost. Used in GPS navigation and game AI.
The CPU always runs the highest-priority ready process. Interrupts are priority-queue events.
Insert the head of each list. Extract min, advance that list, re-insert. O(n log k) total.
Events scheduled by timestamp. Always process the next-in-time event using a min-heap.
Repeatedly merge the two lowest-frequency symbols. A min-heap gives O(n log n) Huffman tree build.
Time complexity
Constant time
Logarithmic time
Logarithmic time
Logarithmic time
🎯 Quick Check
You need to repeatedly extract the smallest element from a dynamic set where new elements are also added. Which is the best data structure?
Priority Queues: summary
Key points
- Sorting the whole array to get the minimum each time is O(n log n); a heap gives O(log n) per operation.
- To find the top K largest items, keep a min-heap of size K: O(n log K).
- JavaScript has no built-in priority queue; write a heap or use a small library.
Frequently asked questions
›Is a priority queue the same as a heap?
A priority queue is the abstract behaviour (get the best item next); a heap is the most common way to implement it.