On Sorting
Reading:
Complete the reading specified in Section B of the Syllabus
Exercises:
Headington and Riley - Chapter Exercises at end of Chapter 12:
#12.2a, c, h, iProgramming Project: [May be done alone or in pairs, but I want to know 1) who was given which tasks at the start and 2) who actually did what they were supposed to do. Turn the program in via e-mail along with a separate e-mail to me describing the process, including items 1 and 2) above. A copy of the paper describing the process also should be turned in to me.]
#12.3
#12.4 a, c, h, i
#12.10 a and d.
Write a program to perform a binary, 4-way (balanced) merge-sort using four files named fileA.dat, fileB.dat, fileC.dat, fileD.dat. You may assume that the following data is initially contained on fileA.dat:
67 71 61 57 59 47 41 43 37 29 31 23 17 19 13 7 11 5 2 3
Here, n = 20. Your algorithm should begin by distributing alternating runs (of length 1) to files fileC.dat and fileD.dat.
/------> fileC.dat
fileA.dat -----><
\------> fileD.dat
Then you perform the first pass of the merge sort, by merging runs of length 1 from fileC.dat and fileD.dat to fileA.dat and fileB.dat.
fileC.dat ------->\
/---------> fileA.dat
(merge) >-------->< (distribute -- while you merge)
fileD.dat ------->/
\---------> fileB.dat
Next, go on to pass 2, which merges runs of length 2 on fileA.dat and fileB.dat back to fileC.dat and fileD.dat. You continue in this fashion until you reach a pass, say pass_total, for which the merge-sort results in all the data on one file totally sorted (with the other file being empty). Note that past_total = ceil(log2(n)), which in our example had better be 5 -- and you should be able to explain why?
Hints:
Within this loop you need a third nested loop which does all the dirty work. This loop (perhaps written as a separate function) merges one run of length len = 2(p-1) from one file, with a run of the same length from the second file. (Note that the length of the resulting merged run is now 2p ). This all works fine until you get to the end of a file. where we will have a problem with the "tail sequence" of a run (that portion of a run on one file that still remains after the end-of-file has been reached on the other file).
We handle these cases in the same way. In case 1, copy the remaining data on file 2 to the file currently being used for output. In case 2, we copy the remaining data on file 1 to the current output file.
The tail copy loop completes a pass.
// Merge a run of length len from streamA and and one from streamB
// to form a run of length 2*len written on streamD.
// Use flags such as inAB and out1.
if (inAB && !out1) inner_merge (len, streamA, streamB, streamD);
fstream streamA, streamB, streamC, streamD;
Remember to pass these streams as type fstream& arguments.
To attach a file for write only, use
and reopen it using
Due Date: To be determined.