CS367 Programming Assignment 5
Why are we doing this program?
In this assignment you will be implementing several sorting algorithms. In addition, each time you run a sorting algorithm, you will be collecting statistics about the sort. The statistics for each sort will include the number of comparisons, the number of data moves, and the time required by the sort. By generating these statistics, we can compare the performance of the different sorting algorithms in practice and see how our results match up with the complexities we derived for each algorithm.
For this assignment, you may use any code or algorithms given in the on-line reading on sorting, in lecture, or in the documentation for the ComparisonSort class. You may not use any other sources to develop or create the code for your sorting algorithms.
The goals of this assignment are to:
What are the program requirements?
The ComparisonSort Class
You will be implementing six sorting algorithms (selection, insertion, merge, quick, heap, and an improved variation on selection sort we'll call selection2) and one method that, given an input array, runs each of the algorithms, generates statistics, and prints out the results. All of these methods will be static (i.e., class) methods in the ComparisonSort class.
Each sort takes a Comparable array as a parameter. Note that nothing is returned. The array that is passed in is modified by the sort so that after the sort is finished the array is in ascending order.
The runAllSorts method is passed an array, runs each of the sort algorithms, and displays the following statistics for each sort:
The sample output shows what is required. All output will go to the console. Note that each sort method will modify the array passed as a parameter. However, in order to compare the behavior of the different sorting algorithms we will want to use the same input for each algorithm. Thus, it is important that the runAllSorts method pass identical information to each sort method. Note also that the runAllSorts method should not modify the original array it is passed.
The SortObject Class
The SortObject class is a class which implements the Comparable interface and is designed to help you count the number of comparisons your code does. It has a class data member that keeps track of the number of comparisons done. Each time the compareTo method is called, the class data member is incremented. The resetCompares method resets the class data member to 0 and the getCompares method returns the number of comparisons done on SortObjects since the last reset. See the SortObject documentation for additional details about this class.
The SortObject class takes care of counting the comparisons. You will just need to make sure that you reset the counter or get its value at appropriate times.
You will need to add all the code to keep track of data moves. For the purposes of this programming assignment, each assignment to a variable of type SortObject is considered one data move. For example, if arr is an array of SortObject, then the code
SortObject temp; temp = arr; arr = new SortObject(8); arr = arr;
does three (3) data moves. You may wish to look at the code for the SortObject class to get ideas for how to keep track of the number of data moves your code does.
To keep track of the time, use the currentTimeMillis method of the System class. This method returns the current time (in milliseconds). To figure out how long a method takes, just get the time right before and right after the method is called and subtract the before time from the after time.
The sample output gives an example of the statistics generated. This output was formatted using a private ComparisonSort.printStatistics that we provide. You are encouraged (but not required) to use it to make your output easier to read.
Analyzing the results
In the file Questions.txt you will find some questions that you will need to answer. These questions are meant to have you think about the results you get from the sorting algorithms you are implementing.
The driver class TestSort has been provided to help you test some of the functionality of your sort algorithms and answer the questions. It takes two command-line arguments:
The sample output was obtained by running TestSort with the command line arguments "5000 43210" You can change this code as you wish to test your entire ComparisonSort class.
You can earn up to 15% extra credit by
Summary of provided materials
How to proceed
After you have read this program page and given thought to the problem we suggest the following steps:
What should be handed in?
Electronically submit the following files to the Program 5 tab on Canvas:
Please turn in only the file named above. Extra files clutter up the "handin" directories.
|Last Updated: 1/11/18 © 2014-18 Beck Hasti and Charles Fischer|