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

Sorting Data That Doesn't Fit

External Sorting 24 minSplit into memory-sized sorted runs, then merge all of them in one pass with a min-heap

dsa / Sorting Data That Doesn't Fit

Explore each scene at your own pace.

01 · Sort a batch bigger than memory

Sort a batch bigger than memory

Sorting Data That Doesn't Fit

Definition
External sorting orders data that is too large for memory by splitting it into chunks that fit, sorting each chunk into a sorted run stored on disk, then merging all the runs in one pass with a min-heap holding only each run's front item.
In plain words
Imagine sorting a huge pile of index cards on a small desk. You sort a desk-sized handful at a time and set each handful aside, then look at the top card of every handful and keep taking the smallest until all the piles are used up.

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.

Three horizontal lanes labelled disk read, in-memory sort and disk write, with staggered blocks: chunk 1 moves into the sort lane while chunk 2 is still being read, and into the write lane while chunk 2 sorts and chunk 3 reads

Watch the merge phase pop the smallest item across three runs, one pass, no re-reading:

step through it
1import heapq
2 
3runs = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
4heap = [(run[0], r, 0) for r, run in enumerate(runs)]
5heapq.heapify(heap)
6out = []
7while heap:
8 value, r, idx = heapq.heappop(heap)
9 out.append(value)
10 print(value, "from run", r)
11 if idx + 1 < len(runs[r]):
12 heapq.heappush(heap, (runs[r][idx + 1], r, idx + 1))
13print(out)

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:

python

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.

Scene 1 of 5
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — external_sort(): Sorts a batch bigger than memory: generate runs, then merge them in one pass.
Scenario Quarry's nightly compactor has more records to sort than fit in memory at once, and re-reading the same data twice is the cost it most wants to avoid.
external_sort.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
external_sort(data, memory_limit) → listdata sorted ascending, simulating memory-sized run generation followed by one k-way merge.
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.