Computer Science 245: Data Structures and Algorithms

Sorting (Due April 19th, 2017)

For your third project, you will implement a number of sorting algorithms, and then test their performance. This project will consist of not just coding, but also testing your code using large data sets. Note that when all of your code is complete and debugged, you still have a fair amount of work to do -- so start early!

Coding Sorting Algorithms

For Part 1 of the assignment, you will need to write 12 sorting algorithms: Your sorting project will contain:
It is critically important that:
Otherwise, the grading program will not function correctly, and you will lose points! Also, check to make sure your sorting algorithms are correct. Several of these algorithms (most notably bucket sort and String-based radix sort) are complicated, and it is easy to make a subtle mistake when coding. Be sure to read the detailed requirements for each sorting algorithm. .

Efficiency Testing

After you have coded your algorithms, you need to test them, to see how long each sorting algorithm takes to run.  You should test each algorithm on both random and sorted lists of sizes 1000, 5000, 10000, 50000, 75000, 100000 and 500000.  You can get a random list using the java class Random, located in java.util.Random  To test the speed of your sorting algorithms, you should use the System.currentTimeMillis() method, which returns a long that contains the current time (in milliseconds). Call System.currentTimeMillis() before and after the algorithm runs, and subtract the two times. Unfortunately,  using currentTimeMillis before and after a function only gives an accurate time estimate if the function takes a long time to run (that is, at least a couple of seconds).  Since running some sorting algorithm on a list of size 1000 will take a very short time, you will need to do something like the following:
long startTime, endTime;
double duration;

Random randomGenerator = new Random();
Sort sorter = new Sort();
startTime = System.currentTimeMillis();
for(i=0;i<NUMITER;i++) 
{
   for (j=0; j< listsize; j++)
       list[j] = randomGenerator.nextInt();

   sorter.quickSort(list,0,listsize-1));
}
endTime = System.currentTimeMillis();
duration = ((double) (endTime - startTime)) / NUMITER;
You'll have to play around with different values for NUMITER -- it will need to change depending upon the size of the list and the algorithm.
You might notice that there is some non-sorting work done in the above algorithm -- mainly, setting up the list before each sort can take place. This work takes a small amount of time in comparison to the sorting, as you can easily check for yourself:
startTime = System.currentTimeMillis();
for(i=0;i<NUMITER;i++) 
{
    for (j=0; j< listsize; j++)
        list[j] = rand();
}
endTime = System.currentTimeMillis();
duration = ((double) (endTime - startTime)) / NUMITER;

You should subtract this setup time from your algorithm running time, to get more accurate results. Your main program may run in either interactive mode or batch mode (though you do not need to implement both modes, just the one that is easiest for you to use in testing.)

Building a Better Sorting Algorithm

When the list get large, quicksort is clearly the fastest comparison sorting algorithm (hence the name). However, when the lists are small enough, quicksort runs slower that some of the Θ(n2) algorithms. This might not seem important until you note that when sorting a large list with quicksort, many many small sublists must be sorted. While the savings on sorting one small list with a faster algorithm is negligible, sorting hundreds of small lists with a faster algorithm can make a difference in the overall efficiency of the sort. For part 3 of the assignment, you will combine quicksort with another sorting algorithm to build the fastest possible sorting algorithm. You have several options --
What does ``small enough'' mean? You can try a percentage of the list (say, 5% or 10%), or an absolute number (8 elements, 10 elements, 15 elements, 100 elements, etc), or something else of your choosing. Your tests should ensure that you have the most efficient algorithm possible. You should also be sure that your hybrid quicksort has reasonable performance on all lists -- most notably, it should be efficient on sorted and inverse sorted lists as well as random lists. Try various methods for choosing the pivot element, to try to get the best possible behavior.

Sorting Algorithms in Detail

What to Turn In

You need to submit to the subversion repository:
In addition to hardcopies, you need to submit all required files to the subversion repository:

https://www.cs.usfca.edu/svn/<username>/cs245/project3/

Put this subversion directory at the top of your printout, to make life easier on the TA.  You do not want the TA to be grumpy when he is grading your code!

Due Date

This project is due midnight on Wednesday, April 19th.

Collaboration

It is OK for you to discuss solutions to this program with your classmates.  However, no collaboration should ever involve looking at one of your classmate's source programs!  It is usually extremely easy to determine that someone has copied a program, even when the individual doing the copying has changed identifier names and comments. Also, DO NOT copy / paste ANY code from any online resource. Start with the provided Sort.java file, and using just your knowledge of the sorting algorithms, write the code.

Supporing Files