A05

Due Date: Thursday, February 26
File(s) to be submitted: RunMerge.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

Modified Merge Sort (More Recursive Sorting)

Summary

Download MergeSort and PrintTrace into a project. Create a new program, RunMerge, as a copy of MergeSort. Remove the method mergeSort and comment out the call to it.

Note: Do not delete the methods called merge. You will need those.

Add new methods to make RunMerge perform as shown in the sample output.

Details

For this assignment I want you to create new methods in RunMerge:

About runMerge

You will call runMerge from main (in the place where mergeSort had been called).

About findRuns

The method findRuns needs to create an array showing where each sorted subsequence begins. Suppose it is given the array:
463552796357229307332990
In this array has three sorted subsequences:
463552796...............
.........357............
............229307332990
The sequences start at locations 0, 3, and 4. The sequences end at 3, 4, and 8.

Remember: the standard way to represent start and end points in an array is to give the index of the first value in the sequence and the index of the first value after the sequence. Thus the first sequence is 0 to 3, the second is 3 to 4, and the last is 4 to 8 (where 8 is the length of the array).
The array we need to return is:
0348

It is (barely) possible that the array will be entirely out of order. For example:
400300200100
would make findRuns return
01234
That means the array you create for this method might need to be as long as arr.length + 1 elements. Since you don't know going in how many runs the given array is going to have, you need to create your array to be one space longer than the array you're given. For the 8-element array further up you would need to create an array of length 9 to hold the start/end positions needed. After you have filled in the start/end positions and thus know how many runs there are, use Array.copyOf to reduce the longer array you created down to one more than the number of runs.

About showRuns

The method showRuns needs to work its way thru the array returned by findRuns printing out the sorted subsequences of the array. Call

PrintTrace.printArray(int[] arr, int begin, int end)
to print out each portion.

As in the sample output, each printed subsequence should be preceded by its number (the index in runStarts where its start is), and it should be followed by a pause.

About reduceRuns

The method reduceRuns needs to take an array of starting/ending points and return an array of revised starting/ending points. The revised array is the one that applies after one round of merging has been applied to the array. For example, merging the sequences 0 to 3 and 3 to 4 results in a sequence from 0 to 4.

Reminder: if there is an odd number of sorted subsequences then the last sequence won't get merged.
Thus, when given the array
0348
this method needs to create and return the array
048

A longer example:
034681016
That should be replaced by
04816

Other Requirements

Make sure your code is formatted properly.

Revise the class javadoc to update the description and add yourself as an author.

Update printIntroduction to print the introduction shown in the sample output, and put your name and A-number in the places reserved.

All new methods should have suitable javadoc comments.

Do not delete or change javadocs for pre-existing methods. (Except delete the javadoc for mergeSort along with the method.)


SUBMIT   /   Check