Skip to the workspace
Pebellslearn by building
Sorting at Scale  ›  Capstone Project

Capstone: Quarry's Compactor

28 minGenerate memory-sized runs, then merge fan_in runs at a time until one remains, counting every disk pass

dsa / Capstone: Quarry's Compactor

Explore each scene at your own pace.

01 · What you're building

What you're building

Capstone: Quarry's Compactor

Definition
A compactor turns a large, growing batch of records into one sorted file using external sorting: it sorts memory-sized runs, then merges a fixed number of runs at a time in rounds until one run remains, counting the disk passes used.
In plain words
It is like tidying many small sorted stacks into one by combining a few at a time, since you cannot hold them all in your hands at once. You keep combining rounds of stacks until only one remains, and note how many times you went through the pile.

In short: Every lesson in this section answered one piece of "sort more, and sort it smarter": heap sort's in-place O(n log n), non-comparison sorts that skip comparing entirely, quickselect's one-sided search, the proof that no comparison sort can do better than n log n in the worst case, and external sorting's runs-then-merge shape for data bigger than memory. Quarry's compactor is where the last piece — external sorting — becomes a real, repeatable job: it doesn't just merge once, it keeps merging in rounds until a growing batch of records collapses into a single sorted file, and it reports exactly how many disk passes that cost.

What you're building

compactor(records, memory_limit, fan_in) -> {"sorted": [...], "runs": r, "passes": p}
compactor([5, 3, 8, 1, 9, 2, 7, 4, 6], 3, 3)
# {"sorted": [1, 2, 3, 4, 5, 6, 7, 8, 9], "runs": 3, "passes": 2}
  • sorted: every record, fully sorted ascending.
  • runs: how many sorted runs records split into after generation — ceil(len(records) / memory_limit).
  • passes: total disk passes — 1 for generation, plus one more per merge round, where each round folds fan_in runs into one until a single run remains.

This is external sorting from the last lesson, generalized: instead of one merge over every run at once, the compactor merges fan_in runs per round — useful when there genuinely isn't room to hold every run's front item open at the same time.

Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — compactor(): Folds a whole day's arrivals into one sorted file, round by round, and reports the disk passes it cost.
Scenario Quarry's nightly compactor folds a whole day's unsorted arrivals into one sorted file, and the ops team wants to know how many disk passes that actually costs before they provision more memory.
compactor.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
compactor(records, memory_limit, fan_in) → dictThe fully sorted records, the run count after generation, and the total disk passes used.
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.