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 (M 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 N records gives about ⌈N/M⌉ runs.
Replacement selection creates longer runs: keep a heap of M 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 2M 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 ⌈log2R⌉ passes for R runs. A k-way merge reads k runs at once, keeps their front records in memory (in a min-heap, so the smallest is found in O(logk)), and needs only ⌈logkR⌉ passes. Only one block per run has to be in memory.
Counting passes
For N records, memory for M and a k-way merge:
passes=1+⌈logk⌈MN⌉⌉.
For example, N=109 records, M=107 and k=100: 109/107=100 runs, merged in ⌈log100100⌉=1 pass, so the whole file is read and written only twice. With 2-way merging it would need 1+⌈log2100⌉=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 runs; about N/2M with replacement selection).
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 6 · short answer
Describe the two phases of external merge sort and explain why k-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, R runs need ⌈log2R⌉ passes over the whole file; a k-way merge (using a min-heap of the k front records) needs only ⌈logkR⌉ 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/searching-and-sorting-techniques/external-sorting/ to read this page.