CS 261 Lab L - Merge Sort

Due Wed Apr 30 at 9:00am

Overview

For this lab, you will practice implementing a sorting algorithm from scratch, based just on the description of the algorithm. Moreover, you will be integrating this sorting algorithm with an existing program--a program that you wrote before!

That's right: you'll be modifying the card flipping lab from way back when to allow the user to shuffle and sort the deck of cards! Furthermore, you'll add a simple "animation" to the sorting, so that you can more easily see it in action.

The particular sorting algorithm you will be implementing is called Merge Sort. This is a recursive sort--that is, we use recursion to do the sorting! This makes it both simple to describe but possibly tricky to implement (in the same way that recursion is always tricky).

This lab will be completed in pairs. Remember to review the pair programming guidelines before you get started!

Objectives

Necessary Files

You will need to download the copy of the SortingCardLab.zip file. and import the included classes into Eclipse. As before, this zip file contains a Card class that is the basis for a playing card, a DrawableCard class that represents a card that can be drawn, a DrawableDeck class to organize all the cards, and a basic CardFrame class to display the deck.

Merge Sort

Merge Sort is a fast sorting algorithm. It is described in detail (with sample code!) on page 441 of the textbook. A description can also be found on Wikipedia. And of course, we will go over the algorithm in lab together.

The basic premise is that if we have two smaller lists (A and B) that are already sorted, then it becomes very easy to combine them together into a single sorted list. You simply compare the smallest item from A and the smallest item from B, and then put that item in your final sorted C list (removing it from A or B as appropriate). Repeat this process until you've added all the items from A and B to C.

But how can we convert an unsorted list into a pair of sorted lists to then merge? We'll first split the list in half, and then sort each half--and we'll sort each half using a merge sort! This is the recursive step--each of those halves get split in half, sorted, and then remerged... we know that we can stop splitting in half when our list has only a single element; this single item is already sorted, so no more recursive is required!

Merge Sort is thus implemented using two methods: the recursive mergeSort() and the helper merge(), which can be described with the following pseudocode:

void mergeSort(originalList):
  if originalList has more than 1 element
    leftList = copy left half of the original list into a new list
    rightList = copy right half of the original list into a new list
    mergeSort(leftList)
    mergeSort(rightList)
    merge(leftList, rightList, original)

void merge(left, right, output):
  while not finished with either list
    compare the front items in both lists
    copy the smaller into the output list
    access the next item from the list who you copied from
  while more elements in left
    copy elements form left into the output list
  while more elements in right
    copy elements fom right into the output list

The actual "sorting" occurs in the merge step; the mergeSort step simply breaks the list down so we know what to merge!

Note that this algorithm is generally not performed "in-place"--that is, you sort items into a new list, which you then make replace your original. We'll be addressing that problem in our implementation.

Again, we will go over this in a bit more detail in lab, and an example implementation can be found in the text.

Assignment Details

There are a number of small pieces to this assignment, described below:

1. Make Cards sortable

The first thing you'll need to do is to make it so we can sort the playing cards! This means they will need to be Comparable. This process should be simple enough--we've done this kind of thing before!

2. Add Shuffle functionality

You'll also need to be able to shuffle the cards so that you can then sort them! Fill in the provided shuffle() method in DrawnDeck.

The moral of the story: Java has lots of utility classes that can help you do common tasks quickly, if you know where to look. This lab is intended to help you find where to look!

3. Implement Merge Sort

Now that the preliminaries are out of the way, on to the main event: implementing the merge sort algorithm. Fill in the provided sort() method in DrawnDeck (important note: this method will act as the launcher for your recursive call; you will need to make separate helper methods!)

As described above, the merge sort algorithm has two parts:

Once you have these methods implemented, you should be able to shuffle and then resort your list!

4. Animate the Sorting

Finally, once you have sorting working, you can add periodic "pauses" in order to slow down the algorithm and see how it works. There are a couple of steps to this:

And that should do it! You should now be able to see your deck sort itelf via merging!

Extensions

One extension you can add if there's time is to "highlight" the cards that are currently being compared and moved. You can do this by marking the particular cards as selected (just like you did with the original flipping lab!) It is possible to earn up to 5 pts of extra credit for adding this extension.

You might also consider adding different sorting algorithms (that also animate in a similar fashion). Be sure and add more buttons to the CardFrame so you can test different sorts! You can earn another 5 pts extra credit for this extension

Submitting

Make sure your name is in a class comment at the top of all the classes you modified and upload them to the LabL submission folder on vhedwig. Make sure you upload your work to the correct folder! The lab is due at the start of class on the day after the lab.

After you have submitted your solution, log onto Moodle and submit the Lab L Partner Evaluation. Both partners need to submit evaluations.

Grading

This assignment will be graded based on approximately the following criteria: