Skip to the workspace
Pebellslearn by building
Sorting at Scale  ›  Lesson

Heap Sort

Heap Sort 24 minBuild a max-heap in the array itself, then repeatedly extract the root into the shrinking sorted suffix

dsa / Heap Sort

Explore each scene at your own pace.

01 · Rank a batch of support tickets

Rank a batch of support tickets

Heap Sort

Definition
Heap sort sorts an array in place by first arranging it as a max-heap, a tree layout where every parent is at least as large as its children, then repeatedly swapping the largest item to the end and repairing the shrinking heap.
In plain words
Picture a tournament where the champion is always at the top. You take the champion out and put them at the back of the line, let the next best player work their way up to the top, and repeat until everyone is lined up in order.

In short: heap sort turns the array itself into a heap, using no extra memory. First it builds a max-heap bottom-up, so the biggest value sits at index 0. Then it repeats one move: swap the root with the last unsorted slot, shrink the heap by one, and sift the new root down to fix the one violation that swap created. Each sift-down costs O(log n), and there are n of them, so the whole sort is O(n log n) time and O(1) extra space — but it is not stable: equal keys can come out in a different order than they went in.

An array of six boxes with curved roof lines connecting index 0 to its children at indices 1 and 2, and index 1 to its children at indices 3 and 4, like a tree drawn flat onto the row it lives in

Rank a batch of support tickets

Quarry's nightly job ranks a batch of support tickets by priority before anyone reads them, highest first. The tickets already sit in one array — no separate structure needed:

python

heap_sort builds ascending order, because each extraction moves the current biggest value to the end. For "highest priority first", read the result backwards, or flip every comparison to build a min-heap instead.

Watch the heap get built, then watch two extractions shrink it from the right:

watch it move · array

Building the heap only fixes parent-child violations, working from the last parent up to the root — most of the array never moves.

Start at the last parent, index 1 (10). Its only child is index 3 (5) — no violation there. 5 cells. At index 1.

1 / 5
watch it move · array

Extraction swaps the root into the shrinking heap's last slot, then sifts the new root back down. The sorted suffix on the right only ever grows.

This is already a valid max-heap: 9 beats both of its children, 5 and 7. 5 cells. At index 0.

1 / 7
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — heap_sort(): Ranks a batch of tickets by priority in place, no extra memory.
Scenario A batch of support tickets lands overnight, and the morning queue needs them ranked by priority before anyone opens the dashboard.
heap_sort.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
heap_sort(arr) → listA new list with arr's values sorted ascending, built with an explicit max-heap.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.