Sorting AlgorithmsC++17

External Sorting

External sorting handles data larger than RAM by sorting memory-sized runs and merging them with sequential disk access.

What to understand

  • Run generation
  • K-way merge
  • Disk I/O cost
  • Buffering

Remember this

  1. 1I/O dominates CPU cost
  2. 2External merge sort is the standard approach
  3. 3Larger merge fan-in reduces passes

Detailed notes

External sorting assumes the dataset is larger than main memory. It reads memory-sized chunks, sorts each chunk into a run, writes runs, and performs buffered multiway merging.

The primary cost is block I/O rather than comparisons. Increasing merge fan-in can reduce the number of full-data passes, but every active run needs buffer space.

How it works

  1. 1Generate the largest sorted runs memory allows.
  2. 2Write runs sequentially to external storage.
  3. 3Use a min-heap to select among run heads.
  4. 4Buffer reads and writes and repeat merging until one run remains.

Where it is used

  • Database ORDER BY and index construction
  • Log and telemetry pipelines
  • Sorting files larger than RAM

Common mistakes

  • Applying an in-memory cost model to disk workloads
  • Using tiny unbuffered reads
  • Creating too many simultaneous runs for available memory

Implementation perspective

This is primarily a conceptual or selection topic. Use the linked implementation topics in this section to see the invariant expressed in C++.

Exam & viva

Open a prompt when you are ready to check your answer.