Project 4: Merge sort

1. Introduction

In this project you will again address the problem of sorting a set of data, this time applying your understanding of recursive methods.

In this project, you will write a program that takes a String and makes a new String with all the same characters, except put in lexicographical order. (Note the difference from last time, when you took an array of integers and rearranged that array.) For example, give then String "jhdmkwl", we would sort this to "dhjklmw". Remember that chars can be compared for lexicographical order using the regular comparison operators.

This project will also walk you through a specific sorting algorithm known as "Mergesort".

2. Set up

Make a directory for this project and copy into it the file MergeSort.java from the class directory.

> cp /homeemp/tvandrun/pub/235/MergeSort.java . 

Notice that the methods mergeSort and merge already exist as stubs (the currently return a dummy value). You will fill-in real code for them.

3. Part I: Merge

We will worry about the actual sorting in Part II. For now, we will write a helper method that will come in handy later. Our goal is to be able to talk two Strings that are already sorted (for example, "acfmq" and "bdglr") and interweaving them (to get "abcdfglmqr").

Write the method merge(String,String) to do this. Note that it has two parameters---it takes two Strings. Merging them is actually somewhat tricky. Think through this carefully first; try a few examples by hand; formulate an algorithm; then write the code.

Meanwhile, document the method. Don't forget to include the one-sentence description (what it does); a couple sentences explaining the algorithm (how it does it); appropriate param flags; and an appropriate return flag. What assumptions are you making about the parameters, besides that they are Strings? This is part of the contract, and should be stated in the documentation.

To test this method, you are going to need to make some changes to the main method. You may want to comment out what is already there (so you can use it later to run the full program); write code that is appropriate for testing just the merge method. Do not go on to Part II until you are sure that merge works right.

4. Part II: MergeSort

Merge sort is a recursive sorting algorithm that works like this (I am using the term "list" to mean "String or array"):

Now that your merge method works, fill-in the body for mergeSort, making use of merge. This requires you to think recursively, but the basic algorithm is already given for you.

5. Hints

Remember the following string methods:

6. Extra credit

It is possible to write merge recursively. You will get bonus points if your merge algorithm is recursive. (However, you definitely should have an iterative version working first; then think about how to rewrite it recursively.)

7. Turn in

Create the script file as before (cat, rm, compile, and run). Run mergesort on six strings, a mixture of short ones and long ones, with an odd number of characters and and even number of characters, including one with only one character. Then print it.

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

DUE: Friday, Oct 13, at 5:00 PM.


Thomas VanDrunen
Last modified: Mon Oct 9 11:05:43 CDT 2006