Overview

11 External-Memory Sorting

Sorting is a core primitive across domains—from geometric algorithms and robotics to databases and bioinformatics—and many problems (like deduplication and element distinctness) reduce to or are accelerated by sorting. When datasets are far larger than RAM, the challenge is to design algorithms that minimize slow disk I/O, measured in block transfers. In the external-memory model, N is the number of records, M is main-memory size, and B is the block size; the overarching strategy is to stream data sequentially, form runs that fit in memory, and combine them with as few full-data passes as possible.

A direct external adaptation of mergesort first makes initial sorted runs of size about M in one sequential pass, then repeatedly merges them. Doing this naively in 2-way fashion takes about (N/B)·log₂(N/B) I/Os, because each merge level requires a full sweep. The key improvement is multi-way merging: keep one buffer block per run and an output buffer, and use a small in-memory heap to pick the next smallest item, allowing up to M/B runs to be merged at once. This M/B-way mergesort reduces the number of passes to log_{M/B}(N/B), achieving an I/O complexity of Θ((N/B)·log_{M/B}(N/B)). The change in logarithm base is substantial here because M and B are parameters, not constants, often yielding order-of-magnitude speedups.

Quicksort can be externalized too: a 2-way version scans the file once per level, partitioning around a pivot with two buffer blocks; when partitions fit in memory, they are sorted internally. To match mergesort’s I/O optimality, a multi-way version finds about M/B “good-enough” pivots in linear I/O by sorting chunks, sampling representatives, and selecting multiple approximate medians, then partitions into O(M/B) buckets in one pass using O(M/B) buffers. This yields the same Θ((N/B)·log_{M/B}(N/B)) I/O bound. Notably, unlike in RAM where balanced search trees equate the per-element costs of searching and sorting, in external memory sorting per element is asymptotically cheaper than B-tree inserts, highlighting the advantage of batching. A matching lower bound—derived by counting how many candidate orderings a single block transfer can eliminate given B-sized blocks and M-sized memory—proves that Θ((N/B)·log_{M/B}(N/B)) I/Os is optimal, making M/B-way mergesort (and multi-way quicksort) asymptotically optimal for external-memory sorting.

: Robot motion planning algorithms often involve computing convex hulls of the nearby objects.
: In bioinformatics, genomes are often ordered according to various criteria. In this particular case, we are ordering genomes by the number of times sequences X and Y have occurred. The importance of sequence X’s presence is valued as twice as that of sequence Y’s presence.
2-way mergesort adapted to external memory. In the first run, elements are processed in batches of size , where each batch of size is sorted. This is our “base case” for external-memory mergesort (internal memory mergesort ordinarily begins from lists of size 1). Then one by one, pairs of lists of size are processed and merged into lists of size , then , and so on. Eventually we arrive at the final list of size .
: Merging sorted lists in external memory. Each list has one representative buffer block residing in memory at all times. The minimum of each block is initially inserted into a heap, from where minima are repeatedly extracted. Every time an element is extracted from the heap, the next element in line from the same block where the minimum came from gets inserted into the heap. When we run out of elements of one particular block, we bring in the next block of the same list.
: In -way mergesort, we begin by creating in one run sorted lists of size . Then these lists are further merged, at one time, to eventually create one sorted list.
: A snapshot during a partition in external- memory 2-quicksort. A data is sequentially input through main memory and each element is compared to the pivot. We have one block of buffer space to accumulate elements smaller than the pivot, and one block of buffer space to accumulate elements larger than the pivot. Once any of the buffer blocks are full, they are written back to a particular location on disk, where either left or the right side of the array is being appended. It is important, for the recursive calls later, that the elements that are smaller/larger than the pivot are all contiguously placed.
: Snapshot of external-memory multi-way quicksort. Instead of one pivot, we find pivots and pull the entire dataset through memory. Based on comparisons with the pivots, each element is routed to the correct buffer block. Once the buffer block of any partition is filled, it is written back to memory and the buffer is emptied. After all data is pulled through main memory, we created partitions that quicksort can again recurse on. Once a partition is of size , the entire partition is read into main memory, sorted, and written back.
: To understand how much one block transfer can help in sorting, we analyze how many potential orderings of B elements (contents of one block) there are inside a memory-full of elements. There is a total of (M choose B) orderings, and this is a factor by which one memory transfer can reduce the total number of permutations remaining to be examined.

 Summary

  • Sorting is one of the best-known problems in computer science, and there is a large body of research optimizing sorting algorithms for different contexts.
  • When data can not fit into main memory, the sorting algorithm needs to bring in small pieces of data into main memory and sort chunk by chunk.
  • -way external mergesort is the algorithm of choice for when data is too large to fit into RAM. This algorithm merges many lists at one, thus making use of a large available memory.
  • Analogously, it is possible to adapt quicksort to work optimally in external memory by choosing a larger set of pivots and thus partitioning data into many individual sub-partitions instead of just 2.

Batched problems like sorting have a cheaper per-element cost than searching in external memory. This is an important difference between RAM and external memory; in RAM, we can optimally sort by inserting into a search structure, while doing that in external memory results with a suboptimal algorithm

  • To understand whether a sorting algorithm is optimal, it is important to understand how sorting lower bounds work. In case of internal memory, the key is understanding how much one comparison can contribute to eliminating permutations that are not the sorted order, and in external memory, we do the same, but by analyzing how much one block input can help us eliminate the permutations.

FAQ

What is external-memory sorting and when do we need it?External-memory sorting is sorting when the data set is too large to fit in RAM, so most data resides on disk. The goal is to minimize disk I/Os by reading and writing data in blocks and organizing the algorithm so it performs few full scans/passes over the data.
What are N, M, and B in the I/O model, and how is cost measured?N = number of records (elements) to sort; M = main-memory capacity (in records); B = disk block size (in records). The cost is the number of block transfers (I/Os). Scanning all data once costs about N/B I/Os.
How does 2-way external mergesort work and what is its I/O complexity?Steps: - Run formation: read M records at a time, sort in RAM, write a sorted run; produces about N/M runs in one pass (≈ N/B I/Os). - Pairwise merging: repeatedly merge runs two at a time using one input buffer block per run (plus output), doing a full scan per merge level. Total merge cost ≈ (N/B) · log2(N/M) I/Os; the initial pass is linear and usually dominated by the merges.
How does M/B-way external mergesort reduce I/Os compared to 2-way merges?By merging up to k ≈ M/B runs at once (one input buffer block per run plus an output buffer). This increases run length by a factor of k each pass, reducing the number of passes to about log_{M/B}(N/M). The optimal total cost is Θ((N/B) · log_{M/B}(N/B)) I/Os.
How is a k-way merge implemented efficiently in RAM?Keep one block buffer for each of the k input runs and one output buffer. Maintain a min-heap of size k containing the current minimum element from each buffer. Refill an input buffer when it empties; flush the output buffer when full. This yields sequential I/O and O(log k) CPU time per output element.
How does external-memory 2-way quicksort partition data on disk?Pick a pivot, then stream the file once. Use two RAM buffer blocks: one accumulates elements ≤ pivot, the other > pivot. When a buffer fills, append it to the correct side on disk. Recurse on the two contiguous partitions. Base case: when a partition fits in RAM (size ≤ M), sort it in memory and write back. With balanced partitions, the I/O cost matches 2-way mergesort: about (N/B) · log2(N/M) I/Os.
Can external quicksort be made multi-way and remain optimal? How are pivots chosen?Yes. Choose t = Θ(M/B) pivots that split the data into roughly balanced partitions. A practical method: - Divide data into chunks, sort each chunk in RAM (during run formation), and take regular samples (representatives) from each chunk. - From these representatives, select t pivots using a linear-time selection routine (e.g., median-of-medians applied recursively on the smaller sample). - Stream the file once, routing each element to one of t+1 partitions using one buffer block per partition. This yields Θ((N/B) · log_{M/B}(N/B)) I/Os, the optimal bound.
Do the pivot partitions need to be perfectly balanced?No. As in internal quicksort, it suffices that partitions are within a constant factor of each other. Using approximate, well-spaced pivots keeps recursion depth within a constant factor of the ideal and preserves the optimal asymptotic I/O complexity.
Why doesn’t the “sorting equals searching” intuition from RAM carry over to external memory?In RAM, inserting N items into a balanced BST costs Θ(N log N), matching optimal comparison sorting. Externally, per-element B-tree inserts cost about Θ(log_B N) I/Os, so N inserts cost much more than sorting, whose per-element I/O cost is only about (1/B) · log_{M/B}(N/B). In other words, batched processing (sorting) exploits large memory and sequential I/O far better than one-by-one searches/inserts.
What is the lower bound for external-memory sorting and what proves it?The information-theoretic lower bound is Ω((N/B) · log_{M/B}(N/B)) I/Os. The proof counts how many candidate permutations remain and how much one block transfer can reduce them, using that a block holds B items and RAM holds M items; each transfer distinguishes at most (M choose B) placements.
In practice, how many passes will I need, and what must fit in RAM during a pass?- Passes: after the initial run formation, the number of merge (or partition) passes is about ceil(log_{M/B}(N/M)). Fewer, larger buffers (larger M/B) mean fewer passes.
- RAM requirements: for k-way merging, need k input buffer blocks + 1 output block + heap overhead; for multi-way quicksort partitioning, need t pivot keys and t+1 buffer blocks. In both cases, k or t is limited to about M/B.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms and Data Structures for Massive Datasets ebook for free