Sort By Merge
File Sort
- 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.
- Sort each F[0,i] in memory, using say g_heap_sort
- Loop:
- Merge these files two at a time, obtaining half as many files
F[1,1] ... F[1,m/2]
- Merge the result files again, two at a time, obtaining half as many files
F[2,1] ... F[2,m/4]
- Continue until at the last step two files are merged into one file F[k,1];
note that k = log2 m
- 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
|