[prev] 27 [next]

External (File) Sorting

Sketch of external mergesort algorithm:

Input: stdin, containing N Items
Output: stream of Items on stdout

copy stdin to A, sorting runs of K items
runLength = K
iter = 0
while (runLength < N) {
   if (iter % 2 == 0)
      inFile = A, outFile = B
   else
      inFile = B, outFile = A
   fileMerge(inFile, outFile, runLength, N)
   iter++;
   runLength *= 2;
}
copy outFile to stdout