| | | | | |

Sort By Merge

File Sort

  1. Break the file F up into subfiles F[0,1] ... F[0,m] small enough to fit into working memory. For simplicity of exposition, assume m = 2k for some k.
  2. Sort each F[0,i] in memory, using say g_heap_sort
  3. Loop:
    1. Merge these files two at a time, obtaining half as many files F[1,1] ... F[1,m/2]
    2. Merge the result files again, two at a time, obtaining half as many files F[2,1] ... F[2,m/4]
    3. Continue until at the last step two files are merged into one file F[k,1]; note that k = log2 m
  4. Rename the sorted file F[k,1] to F

Run time for step 3

  • Inner loop (merge loop) runtime = Θ(n) where n = number of elements in F
  • Outer loop body executes log m times, where m is the number of sets to be merged
  • Note m <= n
  • Runtime of algorithm = Θ(n log m)

Example of "bottom-up" MergeSort


| | Top of Page | 8. Sorting Algorithms - 6 of 16