We have been looking at sorting algorithms and discussing their runtimes. In this assignment, you will empirically verify the relative efficiencies discussed in lecture. You will do this by timing various sorting implementation on different sized arrays and tabulating the results. Using the results, you will comparing the algorithms' performances and come to conclusions about which versions are best.
You will test the following algorithms:
The code for all algorithms above, except #5 (Random Pivot), is already completely written - you only have to change the base case value in the Median of 3 sort from a constant to a variable so that you can give it different values during your program execution. You must write the Random Pivot Quick Sort so that it works correctly. This is actually very similar to the simple Quick Sort, except that you choose the pivot index as a random integer between first and last (inclusive) rather than choosing it as A[last].
Your program should allow the following to be input from the user:
Part of your program may be graded by a program. The prompts of your program must be identical to those shown below: (user input appears in
For each algorithm, your program should iterate through three initial setups of the data:
You will need to time your algorithms. Use the method System.nanoTime()
. This method returns the number of nanoseconds since the JVM started (a nanosecond is 10-9 seconds, i.e. one second is one billion nanoseconds). Timing a trial follows this pattern:
long start = System.nanoTime();
// Execute the sorting method here (array should ALREADY be filled before timing starts)
long finish = System.nanoTime();
long delta_ns = finish - start;
Since you are performing multiple trials, for a given algorithm you will add the times for the trials together, then divide by the number of trials to get the average time per trial. Divide by 1 billion to get your final results in seconds rather than nanoseconds.
For each of the variations in the run, your program must output its results to the file named by the user. Note that since you have 3 data setups and 7 algorithms, each overall execution of your program should produce 21 different results. Each result should look like the following example:
When displaying the algorithm name and order, use the bolded names exactly as shown above. So, "Simple Quick Sort" is valid, but these are not valid: "simple quick sort", "SIMPLE QUICK SORT", "Simple QuickSort", "Simple QS", "Simpl Quik Sort".
In order for your TA to be able to test the correctness of your sorting algorithms and main program logic, you are required to have a Trace Output Mode for your program. This mode should be automatically set when the Array Size is <= 20. In Trace Output Mode, your program should output all of the following to standard output (i.e. the display) for each trial of each algorithm:
The evaluation of the correctness of your algorithms and data processing will be heavily based on the Trace Output Mode for your program. If you do not implement this or it does not work correctly, you will likely lose a lot of credit. Also, be sure that Trace Output Mode is off for arrays larger than 20.
The goal is to see how the runtimes of the algorithms change as the size of the arrays increases. However, actual runtimes will vary based on various factors, including processor speed, memory capacity/speed, and how busy the computer is. Follow the guidelines below for the array sizes. Use 10 trials for all of your runs.
To increase the stack size when running a Java program, include the switch/argument/flag: "-Xss10m", e.g.:
After running that command, the user should be prompted as shown below: (user input appears in
Given that input, your program should produce output similar to test25k.txt. Note: in the output file, the times are randomly generated and do not indicate the runtimes of the algorithms on the input.
After you have finished all of your runs, tabulate your results in a spreadsheet. Use a different worksheet for each initial ordering (Random, Sorted, Reverse Sorted). In each worksheet, make a table of your results with the array sizes as the columns and the algorithms as the rows. Also make a graph for each of your tables so that you can visualize the growth of the runtime for each algorithm. You must also write a brief summary / discussion of your results. Based on your tables, indicate the best algorithm for each of the initial data orderings. Based on your overall results (for all data orderings), speculate on what you think the best of the 7 algorithms is for general purpose use. Your write-up should be well written and justified by your results. Your write-up can be embedded in your spreadsheet or submitted as a separate document (ex: a Word document).
nextInt
method provided by the Random class. Note that there are two nextInt
methods; consider which is more appropriate here.Complete the Assignment Information Sheet.
Make sure you submit all source code files, your spreadsheet of results, and your summary/discussion in a single .zip file to get full credit. Name your main program Assign4.java. Your spreadsheet must be saved in Excel (.xls, .xlsx) or OpenDocument (.ods) format. If you submit your summary/discussion in a separate file, it must be in plain text (.txt), Word (.doc, .docx), or OpenDocument (.odt) format.
Submit your final zip file to CourseWeb in the Assignment 4 folder.
The grading rubric can be found here: Rubric (doc).
The assignment is due Monday, November 16 by 11:59 pm. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded will be the one graded.
If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well. If your question is basically "Are there any problems with my program?" or "Can you check my code?" tell us what you've already done to test your program; provide the output from the test runs of your program.
For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.