Sorting Data That Doesn't Fit
In short: every sort so far assumed the whole array lives in memory at
once. When the data is bigger than RAM, that assumption breaks, and disk
reads become the real cost — not comparisons. External sorting splits the
data into chunks small enough to sort in memory (runs), writes each run
back to disk, then merges all the runs in one pass using a min-heap that
never holds more than one item per run at a time. The heap from the heap
sort lesson comes back here for a different job: picking the smallest of k
candidates, cheaply, over and over.

Watch the merge phase pop the smallest item across three runs, one pass, no re-reading:
Each heappush/heappop costs O(log k) for k runs, and the merge visits
every item exactly once — O(n log k) total, a single sequential pass over
the data instead of n passes of "find the overall minimum".
Sort a batch bigger than memory
Quarry's compactor ingests more records overnight than fit in memory. Split, sort each piece, then merge — the runs never need to be loaded all at once during merging, only their current front item:
With memory_limit = 3 on 9 items, that's 3 runs of 3 — each small enough to
sort with any in-memory sort, then merged with a heap of size 3, never size
9.