CMPSCI 187: Programming With Data Structures

David Mix Barrington

Fall, 2011

Programming Project #6: Merge Sorting Dog Teams

Posted 3 November 2011 due at 11:59 p.m. EDT on Sunday 13 November 2011, by placing .java files and .class files in your cs187 directory on your edlab account. Please place files for this assignment in a subdirectory of cs187 called "project6".

As we get questions on this assignment we will put answers on the Q&A page.

Some useful code is available in this directory on the course web site. We will make a sample driver available within a few days.

Goals of this project:

  1. Use the recursive merge sort algorithm to create a single sorted list out of a number of unsorted linked lists.
  2. Build an iterator for a linked list to allow read-only access to all its members, without using library code.

As on Practice Midterm #2, the managers of the Iditarod sled dog race have asked you for help with a data processing job. They have an array containing the team rosters for each dog team entering the race, each stored as a DogTeam object. (The code for the classes Dog, SledDog, and DogTeam is given in the solutions directory, except for the methods you will need to add. We have added a new int field named teamNumber to the SledDog class.) They would like a single list of all the dogs competing in the race, in a single DogTeam object that is sorted -- in alphabetical order by name, such that multiple dogs with the same name are in order by team number. (The rules forbid two dogs with the same name being on the same team.)

You should implement the Mergesort algorithm from Chapter 8 of L&C, though you cannot use the code there directly since it sorts arrays of comparable elements, not linked lists. Your main tool will be a static method merge in the DogTeam class, which takes two sorted DogTeam objects as parameters and returns a single sorted DogTeam object containing exactly those dogs that are in either input team.

But you also need to create sorted teams from your input unsorted teams, without having side effects on those teams. You are to do this by creating an iterator method for the DogTeam class, such that this method returns an Iterator<SledDog> object for the calling dog team. (Since Iterator<SledDog> is an interface rather than a class, you will have to create an object of some suitable class that implements that interface -- you may find a library class or a class in L&C that will do, or you may write your own.) (Note that your iterator need not implement the optional remove method of the Iterator interface.) Once you have this iterator, create a sorted version of each input team by using the iterator to get each member, then adding that member to a new team using the method addInPlace (which you must also write), which puts its input into the correct place in a sorted DogTeam object.

One of the existing DogTeam methods throws an EmptyCollectionException, so code for this L&C exception class is included. There is also a method in DogTeam to check whether the calling team is sorted according to the compareTo method in SledDog (which you must also write).

You will need to write one missing method in the SledDog class, namely compareTo, and four missing methods in the DogTeam class, namely addInPlace, iterator , merge, and mergeSortTeams. You may also wind up writing your own iterator class.

When you mergesort the teams into one, you should merge them in pairs, then merge the resulting teams in pairs, and so forth until you have one team. If you merge the first two teams, then merge the result with the third team, then the result of that with the fourth team, and so forth, this may be much slower than doing them in pairs like a binary tournament. Here is a hint how to do this. If you have an array of n teams, you could merge 0 with n-1 and put the result in 0, then merge 1 with n-2 and put the result in 1, and so on until you have n/2 or n/2 + 1 teams left. Then you can repeat the process to group the new teams into pairs and merge them so you have about n/4 teams left, and continue through about n/8, n/16, and so on until there is one team.

You may borrow code from any of L&C's classes, or from our solutions, with specific attribution in a comment.

Last modified 6 November 2011