External Sorting
External Sorting
External Sorting
Ta: 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43
A. Sorting of runs:
1. Read 3 (M) records in main memory, quick sort them and store
them on Tb1:
Tb1: 3, 17, 29
2. Read the next 3 records in main memory, sort them and store
them on Tb2
3. Read the next 3 records in main memory, sort them and store
them on Tb1
4, 9, 10 -> 4, 9, 10
4. Read the next 3 records in main memory, sort them and store
them on Tb2
5. Read the next 3 records in main memory, sort them and store
them on Tb1
At the end of this process we will have three runs on Tb1 and two runs
on Tb2:
Tb1: 3, 17, 29 | 4, 9, 10 | 11, 43
B. Merging of runs
Merge the first two runs (on Tb1 and Tb2) and store the result on Ta1.
After merging the first two runs from Ta1 and Ta2, we get a run of length 12,
stored on Tb1:
In each pass the size of the runs is doubled, thus we need [log(N/M)]+1 to get to a run
equal in size to the original file. This run would be the entire file sorted.
In the example we needed three passes (B1, B2 and B3) because [Log(14/3)] + 1 = 3.