Lesson 21 +30 XP

External Sorting: Natural, Balanced & Polyphase Merge

External Sorting: Natural, Balanced & Polyphase Merge

When a file (e.g., 900 MB) is larger than available RAM (100 MB), External Sorting minimizes slow disk/tape I/O passes using two phases:

  1. Run Generation Phase: Read M-sized chunks into RAM, sort internally (e.g., Quick Sort or Replacement Selection with a Min-Heap, which creates runs of average length 2M), and write sorted runs to external storage.
  2. Merge Phase: Merge multiple runs together using an in-RAM k-way merge buffer (e.g., 9-way merge for 900 MB in 100 MB RAM: 9 input buffers + 1 output buffer!).
  1. Two-Way External Merge Sort (N = 14, M = 3)

Suppose Ta1 initially has N = 14 records: [17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43] and RAM holds M = 3 records:

  • Step A (Run Generation): Sort 3-record chunks in RAM and distribute alternately onto Tb1 ([3, 17, 29] | [4, 9, 10] | [11, 43]) and Tb2 ([18, 24, 56] | [6, 36, 45]).
  • Step B1 (Merge Pass 1): Merge pairs from Tb1, Tb2 onto Ta1, Ta2 → runs of length 6.
  • Step B2-B3: Ping-pong between Ta and Tb until 1 sorted run of length 14 remains (⌈ log2 5 ⌉ = 3 merge passes).

2. Natural Merge Sort

Instead of cutting runs at fixed length M, Natural Merge scans the input and preserves pre-existing ascending sequences (natural runs)!

  • Worked Trace on [8, 4, 7, 5, 1, 2, 6, 3]:
  • Natural runs: [8], [4, 7], [5], [1, 2, 6], [3] (5 runs instead of 8!).
  • Distribute alternately to Tape 1 ([8], [5], [3]) and Tape 2 ([4, 7], [1, 2, 6]), then merge pairs repeatedly until 1 sorted run remains.

3. Balanced k-Way Merge

Uses 2k tapes (k input tapes + k output tapes). Every pass reads simultaneously from k tapes (using a Min-Heap of size k to pick the smallest head in O(log k)) and writes merged runs cyclically across k output tapes, reducing total merge passes to ⌈ logk R ⌉ for R initial runs.

4. Polyphase Merge Sort

Uses only k + 1 tapes (e.g., 3 tapes instead of 4 for 2-way merge!) by distributing initial runs unequally according to Fibonacci numbers (e.g., 13 = 8 + 5 runs on Tapes 1 & 2, Tape 3 empty):

  • Phase 1: Merge 5 run-pairs from Tapes 1 & 2 onto Tape 3 → Tape 2 empties; Tape 1 has 3 runs left, Tape 3 has 5 runs.
  • Phase 2: Merge 3 run-pairs from Tapes 1 & 3 onto Tape 2 → Tape 1 empties; Tape 3 has 2 runs, Tape 2 has 3 runs.
  • Phase 3-4: Continue until 1 final sorted run remains-zero wasted tape-copy passes!

Two-Way External Merge Sort: Every Tape, Every Pass (notes)

Input on Ta1 (N = 14, memory M = 3): 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43

StageTape 1Tape 2
A. Run generation (sort 3 at a time, alternate tapes)Tb1: 3,17,29 ; 4,9,10 ; 11,43Tb2: 18,24,56 ; 6,36,45
B1. Runs of 3 → runs of 6Ta1: 3,17,18,24,29,56 ; 11,43Ta2: 4,6,9,10,36,45
B2. Runs of 6 → runs of 12Tb1: 3,4,6,9,10,17,18,24,29,36,45,56Tb2: 11,43
B3. Final mergeTa1: 3,4,6,9,10,11,17,18,24,29,36,43,45,56-

In B1 only Tb1 has a third run (11, 43), so it is simply copied onto Ta1. In B2 the second "pair" is only that one run, so it is copied to Tb2.

Example: 900 MB of Data with 100 MB of RAM

  1. Read 100 MB, sort it in memory (e.g., quick sort), write it to disk as a sorted chunk. Repeat → 900/100 = 9 sorted chunks.
  2. Divide RAM into 9 + 1 = 10 buffers of 10 MB: one input buffer per chunk plus one output buffer.
  3. Do a 9-way merge: repeatedly move the smallest front element among the 9 input buffers to the output buffer.
  • When the output buffer is full, write it to the final file and empty it.
  • When an input buffer empties, refill it with the next 10 MB of its chunk.
  1. In practice a larger output buffer than the input buffers gives better performance.

Balanced Merge with 4 Tapes (notes)

Same 14 records, M = 3. Sorted chunks: [3,17,29], [18,24,56], [4,9,10], [6,36,45], [11,43].

PassTape ATape BTape CTape D
Distribution3,17,29 ; 4,9,10 ; 11,4318,24,56 ; 6,36,45--
Pass 1 (A, B → C, D)emptyempty3,17,18,24,29,56 ; 11,434,6,9,10,36,45
Pass 2 (C, D → A, B)3,4,6,9,10,17,18,24,29,36,45,5611,43emptyempty
Pass 3 (A, B → C)--3,4,6,9,10,11,17,18,24,29,36,43,45,56-

Input and output tapes swap roles after every pass; each pass doubles the run length.

Polyphase Merge with 3 Tapes (notes)

Runs are placed unequally (Fibonacci-like) so that one tape is always empty to receive output:

PhaseTape ATape BTape C
Distribution3,17,29 ; 6,36,45 (2 runs)18,24,56 ; 4,9,10 ; 11,43 (3 runs)empty
1: merge A + B → C (2 merges)empty11,43 (1 run)3,17,18,24,29,56 ; 4,6,9,10,36,45 (2 runs)
2: merge B + C → A (1 merge)3,11,17,18,24,29,43,56empty4,6,9,10,36,45
3: merge A + C → Bempty3,4,6,9,10,11,17,18,24,29,36,43,45,56empty

Each phase stops as soon as one input tape becomes empty; that tape is the output of the next phase. No pass is wasted copying runs between tapes.

Complexity and Trade-offs

  • Run generation: k = n/M chunks, each sorted in O(M log M) → O(n log M).
  • Merging: about log2 k passes of n elements each → O(n log (n/M)).
  • Overall: O(n log n) comparisons, but the real cost is the number of disk/tape passes, which is why k-way, balanced and polyphase merges try to reduce passes.
MethodAdvantageDisadvantage
Two-way (straight) mergeSimple, only 4 tapesMany passes; fixed-length runs ignore existing order
Natural mergeUses existing ascending runs; sorted input needs one passRun lengths are unpredictable
Balanced k-way mergeFewer passes: ⌈ logk R ⌉Needs 2k tapes
Polyphase mergeOnly k + 1 tapes, no copy passesNeeds a Fibonacci-like initial distribution; more complex
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What is External Sorting? Trace Two-Way External Merge Sort on [17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43] with RAM size M = 3. (7.5 Marks)
  • Write: Run-generation + merge phases, show Tb1 and Tb2 initial sorted runs of length 3, then Pass 1 (len 6), Pass 2 (len 12), Pass 3 (len 14).
  • Q2. Differentiate between Natural Merge, Balanced k-Way Merge, and Polyphase Merge. (7.5 Marks)
  • Write: Natural runs ([8,4,7,5,1,2,6,3]), 2k tapes vs k+1 tapes, and Fibonacci run distribution (13 = 8 + 5) in Polyphase Merge.