Listing 14 Pseudocode for an External Sort

/* Program to sort large files externally */

process command-line options:
   -i (ignore case)
   -nW (sort field is numeric, width W)
   -r (reverse sort order)
   -v (verbose mode)
   -N (sort field starts in column N)

if (an input file was specified)
   redirect standard input to file

while (an input line was successfully read)
{
   if (buffer or memory exhausted)
   {
      sort/write lines in memory to temp file
      free memory
   }
   store line in memory
}

if (any temp files were created)
{
   create last temp file from lines in memory
   merge temp files to standard output
      (see Listing 6)
}
else
{
   /* All lines fit in memory */
   sort lines in memory
   print lines to standard output
}

/* End of File */