Presentation on theme: "Sorting Really Big Files Sorting Part 3. Using K Temporary Files Given N records in file F M records will fit into internal memory Use K temp files,"— Presentation transcript:
Using K Temporary Files Given N records in file F M records will fit into internal memory Use K temp files, where K = N / M Create K sorted files from F, then merge them Problems computers compare 2 values at once, not K values merging only 2 of K runs at once creates LOTS of temp files in the illustration on the next page, notice that we soon begin merging small runs with big temp files too many comparisons
Alternative Merging Strategy R1R2 T2 R3T1 R4 F R1R2 T2 R3 T1 R4 F R5T3 R5 T3 empty S1S2 R1 = Run 1 R2 = Run 2 etc What would these trees look like with 8 runs?
N-Way Merge We can create that tree using just 4 temp files 2 are input and 2 are output, the pairs alternate being input and output files Algorithm Write Run 1 into T1 Write Run 2 into T2 Write Run 3 into T1 Write Run 4 into T2... Merge first runs in T1 and T2 into T3 Merge second runs in T1 and T2 into T4 Merge thirds runs in T1 and T2 into T3... Merge first runs in T3 and T4 into T1 Merge second runs in T3 and T4 into T2...