CSE3144 Β· Advanced Data Structures Β· Jul–Nov Semester 2026 Β· Lecture 17 of 36 Β· CO CSE3144.3

Heap Sort & Priority Queue Applications

Lecture 16 built the machine. Today we point it at two different jobs: sort an entire array in place with a guarantee no comparison sort beats, and run the queue that decides who gets served next β€” in a hospital, an OS, or a router.

Dr. Manu ShrivastavaCourse Instructor Β· Consultation Fri 2–5 PM, LHC 308F
~75 minutesSession outcome: apply heaps in sorting and scheduling problems
L17 Β· 00 β€” Agenda ~75 min

Today, part by part

From operations to applications

00–04

Same heap, same BUILD-MAX-HEAP and MAX-HEAPIFY from Lecture 16 β€” two very different jobs.

Why Heap Sort?

04–09

In-place, guaranteed O(n log n) β€” the two things quicksort and merge sort can't both offer at once.

Heap Sort: advantages & limitations

09–14

What you get for giving up stability and cache locality.

Animation: Heap Sort, start to finish

14–32

Sorting the exact heap we built in Lecture 16 β€” 9 rounds, every exchange and every heapify shown.

Priority queues: why, and a real-life example

32–38

A hospital emergency room, triaged by urgency β€” not by who walked in first.

Priority queues: advantages & limitations

38–43

What a heap-backed queue buys you, and what it deliberately gives up.

Animation: the ER triage queue, live

43–55

Five patients, five priorities, arrivals and call-ins interleaved.

More scheduling applications

55–60

OS process scheduling, Dijkstra/Prim, network QoS, A* search.

Complexity, everything together

60–64

Heap sort and priority-queue operations, one table.

Recap & what's next

64–75

Lecture 18: Binomial Heaps.

CSE3144 β€” Lecture 17
L17 Β· 01 β€” From operations to applications ~4 min

Same heap, two different jobs

Last time β€” Lecture 16

We built the machinery: the shape + heap property, the array trick (PARENT/LEFT/RIGHT), BUILD-MAX-HEAP in a verified O(n), HEAP-INSERT (sift-up), and HEAP-EXTRACT-MAX (sift-down). All three run in O(log n) or better.

Today β€” put the machine to work
  • Heap Sort: repeat EXTRACT-MAX exactly n−1 times on the same array β€” no extra memory, and the array sorts itself in place.
  • Priority queues: wrap HEAP-INSERT and EXTRACT-MIN/MAX behind a scheduling interface β€” "always serve the most urgent item next," whatever "urgent" means for your problem.
CSE3144 β€” Lecture 17 Β· From operations to applications
L17 Β· 02 β€” Why Heap Sort? ~5 min

Guaranteed O(n log n), and not one extra byte of memory

The gap it fills

Quicksort is fast in practice but has an O(n²) worst case on adversarial input. Merge sort guarantees O(n log n) but needs O(n) extra space for the merge step. Heap Sort guarantees O(n log n) and sorts in place β€” the one combination neither of the other two offers simultaneously.

The idea, in one line

BUILD-MAX-HEAP once (Lecture 16, O(n)). Then repeat: swap the root (the current maximum) with the last element of the still-unsorted region, shrink that region by one, and MAX-HEAPIFY the new root back into place. Do this n−1 times and the array is sorted, built entirely from the back forward.

Real-world relevance

Anywhere memory is tight and a worst-case guarantee matters more than average-case speed β€” embedded systems, real-time systems with hard memory budgets, or any library sort that must never degrade to O(n²) regardless of input β€” Heap Sort (or an introspective hybrid that falls back to it) is the standard answer.

CSE3144 β€” Lecture 17 Β· Why Heap Sort
L17 Β· 03 β€” Heap Sort: advantages & limitations ~5 min

What guaranteed in-place sorting costs you

Advantages
  • Guaranteed O(n log n) in the worst case, every time β€” no adversarial input degrades it, unlike quicksort.
  • O(1) extra space β€” sorts in place, unlike merge sort's O(n) auxiliary array.
  • Builds directly on operations you already trust β€” no new machinery beyond BUILD-MAX-HEAP and MAX-HEAPIFY from Lecture 16.
Limitations
  • Not stable. Equal-priority elements can be reordered relative to each other β€” the swap-with-last step has no memory of original relative order.
  • Poor cache locality in practice. MAX-HEAPIFY jumps between indices i, 2i, 2i+1 β€” scattered memory access compared to quicksort's mostly-sequential partitioning, so heap sort is often slower in practice despite an equal or better asymptotic bound.
  • Not adaptive. An already-sorted (or nearly-sorted) input gets no speed-up at all β€” heap sort does the same O(n log n) work regardless of how "easy" the input is, unlike insertion sort or adaptive variants of merge sort.
CSE3144 β€” Lecture 17 Β· Heap Sort trade-offs
L17 Β· 04 β€” Animation: Heap Sort ~18 min

Sorting the exact heap we built last lecture β€” one extraction at a time

Starting point: [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] β€” the max-heap Lecture 16 built from [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]. Each of the 9 rounds: exchange the root with the last element of the active heap (teal cells are already locked into their final sorted position and never touched again), shrink the heap, then MAX-HEAPIFY the new root. Nothing is skipped β€” every round is shown, including the cheapest ones.

Interactive β€” HEAPSORT(A), 9 rounds
CSE3144 β€” Lecture 17 Β· Heap Sort animation
L17 Β· 05 β€” Priority queues: why, and a real-life example ~6 min

A hospital emergency room doesn't serve first-come, first-served

The real-life example

Walk into an ER and it doesn't matter who arrived first. A triage nurse assigns each patient an urgency level; the most urgent waiting patient is always seen next, no matter how long anyone else has been sitting there. A patient arriving in cardiac arrest jumps ahead of five people who arrived hours earlier with a sprained ankle.

The abstract version

That's exactly a priority queue: an abstract data type supporting INSERT (a new item arrives, with a priority) and EXTRACT-MIN/MAX (serve the most urgent item waiting). It is a generalization of a plain queue (FIFO, priority = arrival time) and of a plain stack (LIFO) β€” a binary heap is simply the standard, efficient way to implement it.

Why a heap, specifically

A priority queue needs exactly the two operations a binary heap is built for: insert a new arrival (sift-up, O(log n)) and extract the current extreme (sift-down, O(log n)). No other operation is required β€” which is precisely why a heap, and not a sorted list or a balanced BST, is the standard choice: it's the simplest structure that does exactly this and nothing more.

CSE3144 β€” Lecture 17 Β· Why priority queues
L17 Β· 06 β€” Priority queues: advantages & limitations ~5 min

Exactly the right abstraction for "serve the most urgent next" β€” and only that

Advantages
  • O(log n) arrival and service β€” scales to large, constantly-changing queues without ever re-sorting everything.
  • Priority, not arrival order, decides service β€” modeling urgency directly, rather than bolting it onto a FIFO queue with manual re-ordering.
  • O(1) peek at "who's next" without removing them β€” useful for dashboards/monitoring ("what's the most urgent item right now?").
Limitations
  • No fairness guarantee. A steady stream of high-priority arrivals can starve low-priority items indefinitely β€” a real operational risk in scheduling systems, usually solved by aging (gradually increasing a waiting item's priority over time), which a plain heap does not do automatically.
  • Changing an item's priority is expensive without extra bookkeeping (Lecture 16's decrease-key limitation) β€” if a waiting patient's condition worsens, updating their position needs an auxiliary index, not just a heap.
  • No efficient "list everyone waiting, in order" β€” that requires repeated extraction (effectively a heap sort), not a cheap traversal.
CSE3144 β€” Lecture 17 Β· Priority-queue trade-offs
L17 Β· 07 β€” Animation: the ER triage queue, live ~12 min

Five patients, five priorities, arrivals and call-ins interleaved

Triage levels 1 (most critical) through 5 (least urgent) β€” a min-priority queue, since the lowest number is the most urgent. Watch what happens when a critical patient arrives after two others have already been waiting.

Interactive β€” ER triage queue: arrivals (INSERT) and call-ins (EXTRACT-MIN)
CSE3144 β€” Lecture 17 Β· ER triage animation
L17 Β· 08 β€” More scheduling applications ~5 min

The same pattern, well beyond hospitals

OS process scheduling

Operating systems keep a ready-queue of processes keyed by priority (and often dynamically adjusted to prevent starvation) β€” "run the highest-priority ready process next" is the priority-queue problem verbatim.

Dijkstra & Prim (Lecture 16 callback)

Both algorithms repeatedly pick the cheapest available vertex/edge β€” implemented with exactly the min-priority queue we just animated, typically a binary heap, upgraded to a Fibonacci heap (Lecture 19) when decrease-key dominates the running time.

Network packet scheduling (QoS)

Routers prioritize latency-sensitive traffic (voice/video) over bulk transfers using priority queues on packets β€” the same "urgent jumps the line" behavior as the ER example.

A* search

Pathfinding's "open list" is a min-priority queue ordered by estimated total cost β€” always expand the most promising node next, exactly like always calling the most urgent patient next.

Printer / job queues

Print spoolers and batch job systems often support priority levels (e.g. a rush job) β€” small jobs or urgent flags jump ahead of a long queue of routine ones.

Event-driven simulation

Discrete-event simulators keep a priority queue of future events ordered by timestamp β€” "process the next event in time order" is a priority queue keyed on time instead of urgency.

CSE3144 β€” Lecture 17 Β· Real-world use
L17 Β· 09 β€” Complexity, everything together ~4 min

Heap Sort and priority-queue operations, one table

OperationCostVerified today as
BUILD-MAX-HEAP (once, at the start)O(n)from Lecture 16 β€” reused as-is
HEAPSORT total (n−1 rounds of exchange + MAX-HEAPIFY)O(n log n)9 rounds, 23 total swap operations on 10 elements
HEAPSORT extra spaceO(1) β€” sorts in placeonly the array itself, plus O(1) temporaries for each swap
Priority queue INSERTO(log n)each ER arrival β€” at most 1–2 sift-up comparisons in a 3-patient queue
Priority queue EXTRACT-MIN/MAXO(log n)each ER call-in β€” at most 1 sift-down comparison in a 3-patient queue
Priority queue PEEKO(1)"who's next" without removing them β€” always the root
CSE3144 β€” Lecture 17 Β· Complexity
L17 Β· 10 β€” Recap & what's next ~10 min

One heap, two jobs β€” sort everything, or serve the most urgent

Lecture 18 β€” next

Binomial Heaps

What a binary heap can't do at all: merge two heaps together efficiently. Binomial heaps are built specifically to fix that.

Homework β€” bring to Lecture 18
  • Run HEAPSORT by hand on the max-heap you built for last lecture's homework array. Show the array after every round and count total swaps.
  • Design a min-priority-queue trace for a print-spooler scenario: 4 jobs arrive with page counts as priority (fewer pages = more urgent), with at least one job arriving mid-queue that should jump ahead of an earlier one. Show the queue after each event.
  • In 3–4 sentences: explain why HEAPSORT is not a stable sort, using a concrete example with two equal-priority elements that could end up reordered.
  • In 3–4 sentences: what does "starvation" mean for a priority queue, and sketch (in words, no code needed) how "aging" β€” slowly increasing a waiting item's priority over time β€” could prevent it.
CSE3144 β€” Lecture 17

Questions?

Dr. Manu Shrivastava β€” LHC 308F β€” Friday 2:00–5:00 PM

Next: Lecture 18 β€” Binomial Heaps.