Priority Queue
The most urgent item leaves firstA priority queue is like the emergency room: patients are not treated in the order they arrived, the most urgent one goes first. Every item is a value with a priority. Try the two classic ways of keeping the items in an array: sorted (enqueue does the work) and unsorted (dequeue does the work).
- Enqueueadd a value + its priority
- Dequeueremove the highest priority
- Same priorityfirst come, first served
- Two wayssorted or unsorted array
- Best so far
- Compared
- New / found
- Leaving
- Empty
Ready
Items are written value:priority (value up to 3 letters or digits, priority 1–9, up to 8 items) · Keyboard: ← → step · Space play / pause · R reset
Every operation, in plain steps
These are exactly the steps the visualizer animates.
An item is a value with a priority
Every item stores a value and a priority. The items sit in an array, packed from index 0, and n counts them. A rule decides which priority number is served first: the LOWEST number (priority 1 = most urgent) or the HIGHEST. Items with the same priority are served in the order they arrived, first come, first served. Only one small test, better(a, b), knows about the rule.
#define CAPACITY 8
struct Item { int value; int p; }; // p = the priority
struct Item item[CAPACITY]; // the array, packed from index 0
int n = 0; // how many items
// better(a, b): does priority a leave before priority b?
int better(int a, int b) { return a < b; } // lowest number first (use a > b for highest first)
Enqueue (sorted array)
- Check there is room: n < CAPACITY.
- Start at the right end and move left.
- Shift every item that leaves before (or with) the new one one place right.
- Put the new item in the gap: the array stays sorted.
Dequeue (sorted array)
- The item that leaves next is at the right end.
- Take item[n − 1].
- Make n smaller — nothing has to shift.
- So dequeue is O(1).
Enqueue (unsorted array)
- Check there is room: n < CAPACITY.
- Put the new item at index n.
- Make n bigger.
- No searching and no shifting: O(1).
Dequeue (unsorted array)
- Look at every item, keeping the best one found so far.
- On a tie the earlier item stays the best.
- Remove it and shift the later items one place left.
- Searching and shifting make it O(n).
Equal priorities
- Items with the same priority leave in arrival order.
- Sorted array: a new item goes to the LEFT of its equals.
- Unsorted array: the scan keeps the earlier item.
- That is what makes the queue fair.
Peek, Is Empty, Is Full
- Peek (sorted array): look at the right end, O(1).
- Peek (unsorted array): scan for the best, O(n).
- Is Empty: n == 0. Is Full: n == CAPACITY.
- None of them changes the queue.
Ascending or descending
- Ascending priority queue: the smallest number is served first.
- Descending priority queue: the largest number is served first.
- Only the better(a, b) test changes.
- Everything else works exactly the same.
A faster way: the heap
- Each array makes one operation slow: O(n).
- A binary heap makes BOTH enqueue and dequeue O(log n).
- Schedulers and Dijkstra's algorithm use a heap.
- See the Heaps topic.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of items. With an array, one of the two main operations is always O(n): the sorted array pays when it enqueues, the unsorted array pays when it dequeues. A binary heap pays only O(log n) for both.
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Enqueue (sorted array) | O(1) |
O(n) |
O(n) |
Grows with n |
| Dequeue (sorted array) | O(1) |
O(1) |
O(1) |
Instant |
| Enqueue (unsorted array) | O(1) |
O(1) |
O(1) |
Instant |
| Dequeue (unsorted array) | O(n) |
O(n) |
O(n) |
Grows with n |
| Peek (sorted / unsorted) | O(1) / O(n) |
O(1) / O(n) |
O(1) / O(n) |
Grows with n |
| Enqueue or dequeue (heap) | O(log n) |
O(log n) |
O(log n) |
Grows with n |
Sorted array versus unsorted array
| Sorted array | Unsorted array | |
|---|---|---|
| Enqueue | O(n): shifts items to open a gap | O(1): adds at the end |
| Dequeue | O(1): takes the right end | O(n): scans, then shifts |
| Peek | O(1) | O(n) |
| Where the next item is | always at the right end | anywhere: it has to be found |
| Best when | you dequeue and peek a lot | you enqueue a lot |
Queue versus priority queue
| Queue | Priority queue | |
|---|---|---|
| Who leaves first? | the oldest item | the highest priority |
| An item stores | a value | a value and a priority |
| Equal priorities | not needed | first come, first served |
| Typical uses | printer line, breadth-first search | CPU scheduling, emergency room, Dijkstra's algorithm |
Check your understanding
Answer each question — you will see the explanation straight away.
Items arrive: A (priority 2), B (priority 1), C (priority 3). With "lowest number first", which is dequeued first?
Priority 1 is the most urgent, so B leaves first, then A, then C — whatever the order they arrived in.
A and B both have priority 2, and A arrived first. Which one leaves first?
Equal priorities are served first come, first served, so the older item, A, leaves first.
In a SORTED array priority queue, where is the item that leaves next?
The array is kept sorted so that the best item is at the right end: dequeue just takes item[n − 1].
How long does enqueue take in an UNSORTED array priority queue?
The new item simply goes into the first free slot: no searching and no shifting.
Which operation of an unsorted array priority queue has to look at every item?
The most urgent item could be anywhere, so dequeue scans all n items to find it (and then shifts the later ones left).
Which structure makes both enqueue and dequeue O(log n)?
A heap keeps just enough order to find the best item at the root and repair itself in O(log n).
In a DESCENDING priority queue, which item is served first?
A descending priority queue serves the largest number first (an ascending one serves the smallest first).
Queues quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.