MCS-208 · Unit 9

Searching and Sorting Techniques

Section 9 of 9

Block 3 · Unit 9Searching and Sorting Techniques8 min · Section 9 of 9

9.9External Sorting

Covers IGNOU MCS-208 §Programme guide topic "External Sorting Algorithms" (not in the Unit 9 booklet)

All the methods so far assume that the whole file fits in main memory. A file of several hundred gigabytes on disk does not. External sorting sorts such files by working on pieces that fit in memory and merging them, while keeping the number of slow disk reads and writes small.

The cost model

Reading or writing a disk block takes thousands of times longer than a comparison in memory, so the cost of an external sort is counted in passes over the data (each pass reads and writes the whole file once).

Phase 1: creating runs

Read as many records as fit in memory (MM records), sort them with an internal sort (e.g. quick sort or heap sort), and write them out as a sorted run. Repeat until the input is used up. A file of NN records gives about ⌈N/M⌉\lceil N/M \rceil runs.

Replacement selection creates longer runs: keep a heap of MM records; repeatedly output the smallest record that is not smaller than the last record output, and replace it with the next input record. On random input, runs are about 2M2M long on average, so fewer runs are produced.

Phase 2: merging runs

Merge the runs as in merge sort, but from disk. A 2-way merge combines two runs at a time and needs ⌈log⁡2R⌉\lceil \log_2 R \rceil passes for RR runs. A k-way merge reads kk runs at once, keeps their front records in memory (in a min-heap, so the smallest is found in O(log⁡k)O(\log k)), and needs only ⌈log⁡kR⌉\lceil \log_k R \rceil passes. Only one block per run has to be in memory.

Counting passes

For NN records, memory for MM and a kk-way merge:

passes=1+⌈log⁡k⌈NM⌉⌉.\text{passes} = 1 + \left\lceil \log_k \left\lceil \frac{N}{M} \right\rceil \right\rceil.

For example, N=109N = 10^9 records, M=107M = 10^7 and k=100k = 100: 109/107=10010^9 / 10^7 = 100 runs, merged in ⌈log⁡100100⌉=1\lceil \log_{100} 100 \rceil = 1 pass, so the whole file is read and written only twice. With 2-way merging it would need 1+⌈log⁡2100⌉=81 + \lceil \log_2 100 \rceil = 8 passes.

Key takeaways

  • External sorting is used when the file does not fit in main memory.
  • Phase 1: sort memory-sized pieces into runs (≈N/M\approx N/M runs; about N/2MN/2M with replacement selection).
  • Phase 2: merge runs; kk-way merging needs ⌈log⁡kR⌉\lceil \log_k R \rceil passes.
  • Cost is measured in passes over the disk, not in comparisons.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

A file of 1,000,000 records is sorted with memory for 10,000 records. How many runs does phase 1 produce (without replacement selection)?

Show answer

Answer: B. 100

⌈106/104⌉=100\lceil 10^6 / 10^4 \rceil = 100 runs. Replacement selection would give about 50.

Question 2 · multiple choice

How many 2-way merge passes are needed to merge 100 runs into one?

Show answer

Answer: D. 7

⌈log⁡2100⌉=7\lceil \log_2 100 \rceil = 7 (runs: 100, 50, 25, 13, 7, 4, 2, 1).

Question 3 · multiple choice

How many merge passes does a 10-way merge need for the same 100 runs?

Show answer

Answer: A. 2

⌈log⁡10100⌉=2\lceil \log_{10} 100 \rceil = 2 (100 → 10 → 1).

Question 4 · multiple choice

What is the main advantage of replacement selection?

Show answer

Answer: C. It produces runs about twice as long as memory on random input, so there are fewer runs to merge.

Fewer, longer runs mean fewer merge passes. Merging is still required.

Question 5 · multiple choice

What does this program print?

C · q9-passes.c
#include <stdio.h>

int main(void)
{
    long N = 1000000, M = 1000, k = 10;
    long runs = (N + M - 1) / M;          /* ceil(N / M) */
    int merge_passes = 0;
    while (runs > 1) {                    /* k-way merge */
        runs = (runs + k - 1) / k;
        merge_passes++;
    }
    printf("runs=%ld merge passes=%d\n", (N + M - 1) / M, merge_passes);
    return 0;
}
Show answer

Answer: C. runs=1000 merge passes=3

1000 runs → 100 → 10 → 1 with 10-way merging: 3 passes. 2-way merging would need 10.

Question 6 · short answer

Describe the two phases of external merge sort and explain why kk-way merging is used.

Show worked answer

Phase 1 (run creation): read the file in pieces that fit in main memory, sort each piece with an internal sort (or use replacement selection with a heap to get longer runs), and write each sorted piece to disk as a run. Phase 2 (merging): repeatedly merge runs into longer runs until one run, the sorted file, remains; only the current block of each input run and one output block need to be in memory. With 2-way merging, RR runs need ⌈log⁡2R⌉\lceil \log_2 R \rceil passes over the whole file; a kk-way merge (using a min-heap of the kk front records) needs only ⌈log⁡kR⌉\lceil \log_k R \rceil passes. Since each pass reads and writes the entire file on disk, which is far slower than computing, reducing the number of passes is the main goal.

↑ ↓ move↵ openTab switch type