CS 211 — Homework Four

Sorting out sorting

Due: 27 February 2013, 11:59p

Worth: 80 points

Part one: Implementation

For this assignment we are going to step back briefly from data structures and implement some sorting functions. I would like you to create a class called Sorter, which will contain all of your code. We are modeling this class on the Collections class from the Java API, which provides the sorting facilities to the implementations of the List interface. As such, your sorting algorithms should be able to sort any List of comparable items. [Yes, we are breaking back into the Java API for this one — you should support the actual List interface from java.util, so that means your code should work on ArrayLists, LinkedLists, Vectors, etc...]

Within this class, you will need to implement the following methods:

There are a couple of things to notice here. First, all of these methods are static — there is no state associated with the functions, they should take in a list and sort it in place (you will have to do some work for mergeSort() to make it appear to happen in place). The second thing to notice is the funny <T extends Comparable<? super T>>. This basically means that our T needs to be comparable (this should make sense — how would we sort a list of objects if we have no way to determine if one is "bigger" than another?). In the Java API, however, this means something quite specific. It means that the object implements the Comparable interface, and has a compareTo() method defined for it (e.g., Integer yes. int no. Fortunately, auto-boxing will help us out here). If you are not familiar with compareTo() and how it works, take a look in the API.

Part two: The Sort Off

Once the sorting algorithms are all working, I want you to take a look at their behavior. You will load up a list with some random Integers and sort it using the three different sorting algorithms and seeing how long each one takes to run. To get meaningful results, all three algorithms should work on exactly the same list (not the same Object, of course, but three lists with exactly the same contents in the same order). There are a number of ways to do this. You could use the clone() method, you could copy the same values into all three lists as you generate them, or you could use what you know about Random and seed values to make sure the three lists are loaded with the same values. Do not just use an assignment statement to "copy" the list to a new variable name.

You can do the timing by taking the difference between calls to System.currentTimeMillis() on either side of calling your methods. The times should be reported to the user using System.out.println, and should look exactly like method name: elapsed time. It is important that you actually look at these numbers and make sure they make sense to you! If you find that merge sort is running at the same speed as insertion sort, there is an error somewhere that you need to address.

I would like you to put this in the main method of a class called AssignmentFourMain. You will write main to accept an input when called on the command line. This input should determine the size of the list to be sorted. This will allow you to experiment with different size lists to see how it affects the runtime. So, I should be able to start your program by typing java AssignmentFourMain 1000 on the command line and it will tell me the result of running the three algorithms on a list of length 1000. In Eclipse, you can't start things from the command line, but you can still pass command line arguments through the project properties.

You DO NOT need to write JUnit tests for this method. Hopefully I will also have figured out how to get Web-CAT to ignore the class so it won't count against your code coverage.

Grading

PointsItemComments
Problem and Code Coverage
15 ptsInsertion sortAll three sort algorithms will be graded on behavior (does it sort correctly) and implementation (is it actually an implementation of the given algorithm)
20 ptsQuick sort
20 ptsMerge sort
15 ptsmain
Misc
10 ptsCommentsAll methods and classes should have Javadoc style comments and non-trivial code should have explanations.

Book keeping

As I said in class, I would like an idea how much time you are spending on this and where the time is going. So please log the amount of time that you spend on:

Try to be as accurate as possible (it would be great if you didn't just get to the end and go "oh right, I need to keep track of my time & well it seemed like five hours..."). I am using this to (a) figure out if you (as a class, and also on a personal level) need more direction on how to direct your energies, and (b) inform the shape of future assignments. The more accurate this information is, the better I can do those two things, so please do not exaggerate in an attempt to get me to trim the assignments down or underestimate because you feel like your classmates didn't need as much time as you. Please include these in a comment at the top of the AssignmentFourMain class.


Last modified: Thu Feb 21 14:47:18 EST 2013