External Sorting
Example of Two-Way Sorting:
N = 14, M = 3 (14 records on tape Ta1, memory capacity: 3 records.)
Ta1: 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43
Example of Two-Way Sorting:
N = 14, M = 3 (14 records on tape Ta1, memory capacity: 3 records.)
Ta1: 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43
- Sorting of runs:
- Read 3 records in main memory, sort them and store them on Tb1:
- Read the next 3 records in main memory, sort them and store them on Tb2
- Read the next 3 records in main memory, sort them and store them on Tb1
- Read the next 3 records in main memory, sort them and store them on Tb2
- Read the next 3 records in main memory, sort them and store them on Tb1
- Merging of runs B1. Merging runs of length 3 to obtain runs of length 6.
Tb1: 3, 17, 29
Tb2: 18, 24, 56
Tb1: 3, 17, 29, 4, 9, 10
Tb2: 18, 24, 56, 6, 36, 45
(there are only two records left)
Tb1: 3, 17, 29, 4, 9, 10, 11, 43
Tb1: 3, 17, 29 | 4, 9, 10 | 11, 43
Tb2: 18, 24, 56 | 6, 36, 45 |
Tb1: 3, 17, 29 | 4, 9, 10 | 11, 43
Tb2: 18, 24, 56 | 6, 36, 45 | Thus we have the first two runs on Ta1 and Ta2, each twice the size of the original runs:
Next we merge the third runs on Tb1 and Tb2 and store the result on Ta1. Since only Tb1 contains a third run, it is copied onto Ta1:
The second set of runs is only one run, copied to Tb2
Now on each tape there is only one run. The last step is to merge these two runs and to get the entire file sorted.
Number of passes: log(N/M)
In the example we needed three passes (B1, B2 and B3) because [Log(14/3)] + 1 = 3.
No comments:
Post a Comment