External Sorting: Example of multiway external sorting
Ta1: 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43
Assume that we have three tapes (k = 3) and the memory can hold three records.
Ta1: 17, 3, 29, 56, 24, 18, 4, 9, 10, 6, 45, 36, 11, 43
Assume that we have three tapes (k = 3) and the memory can hold three records.
- Main memory sort
- Merging B.1. Merging runs of length M to obtain runs of length k*MIn our example we merge runs of length 3
- We build a heap tree in main memory out of the first records in each tape.
These records are: 3, 18, and 4. - We take the smallest of them - 3, using the deleteMin operation,
and store it on tape Ta1.
The record '3' belonged to Tb1, so we read the next record from Tb1 - 17, - The next deleteMin operation will output 4, and it will be stored on Ta1.The record '4' comes from Tb3, so we read the next record '9' from Tb3
and insert it into the heap.
Now the heap contains 18, 17 and 9. - Proceeding in this way, the first three runs will be stored in sorted order on Ta1.
- We build a heap of the first elements of the two tapes - 3 and 6,
and output the smallest element '3' to tape Tb1. - Then we read the next record from the tape where the record '3' belonged - Ta1,
and insert it into the heap. - Now the heap contains 6 and 4, and using the deleteMin operation
the smallest record - 4 is output to tape Tb1.
The first three records are read into memory, sorted and written on Tb1,
the second three records are read into memory, sorted and stored on Tb2,
finally the third three records are read into memory, sorted and stored on Tb3.Now we have one run on each of the three tapes:
Tb2: 18, 24, 56
Tb3: 4, 9, 10
and stored as the second run on Tb1:
Thus, after the main memory sort, our tapes look like this:
Tb1: 3, 17, 29, | 6, 36, 45,
Tb2: 18, 24, 56, | 11, 43
Tb3: 4, 9, 10
and the resulting runs would be of length 9.
and insert it into the heap. Now the heap contains 18, 4, and 17.
Now it is time to build a heap of the second three runs.
(In fact they are only two, and the run on Tb2 is not complete.)
The resulting sorted run on Ta2 will be:
Ta2: 6, 11, 36, 43, 45
This finishes the first pass.
Proceeding in this way, the entire file will be sorted on tape Tb1.
In the example this is [log3(14/3)] + 1 = 2.
No comments:
Post a Comment