Be sure you are acquainted with my collaboration policy and my late policy, both found on the main course webpage. If you are working with a partner, be sure to include both of your names.
In this assignment, you will implement three sorting algorithms--two you've seen before (InsertionSort and QuickSort) and one new one (BubbleSort). I have written some skeleton code containing method signatures for the three sorts, some helper methods, and a main method. Here is the file:
After implementing the algorithms, you will test their runtimes to see if they are different. (Hint: They are different.)In bubble sort, the idea is to make repeated passes through the array, comparing adjacent elements and "bubbling" the greatest element all the way to the top of the array. Once this has been done once, the greatest element is in its proper position, and the next pass through the array need only make comparisons up to the second to last element. The next pass need only bubble up to the third to last element, and so on.
Here is an example:
Bubble the greatest element:Describe the runtime of this algorithm in the comments of your implementation. If you have any questions about it, let me know.
Read through SortTest.java
and familiarize yourself with what it is doing. It contains the following methods:
main()
- the main function. Currently it just runs timeTest(). Feel free to add your own tests while debugging, but this method should be unchanged in your final file.
timeTest()
- tests the timing of the various sorting algorithms. Also checks to make sure they are actually sorting the lists.
isSorted()
- runs through a given list and returns true if it is sorted, false otherwise. YOU NEED TO IMPLEMENT THIS.
bubbleSort()
- performs a bubble sort on a given list. YOU NEED TO IMPLEMENT THIS.
insertionSort()
- performs an insertion sort on a given list. YOU NEED TO IMPLEMENT THIS.
quickSort()
- performs a quick sort on a given list. YOU NEED TO IMPLEMENT THIS.
swap()
- swaps the values in two given indices of a given array. Should be useful for your sorting methods.
arrayString()
- returns a string representation of a given array. May be useful for debugging.
Your job for this assignment is to implement isSorted()
, bubbleSort()
, insertionSort()
, and quickSort()
. Remember to explain the run-time of BubbleSort in the comments of its implementation. Most of these we have already done in class--this is not meant to be an extraordinarily difficult assignment. You may want to create private helper methods. For instance, in class, we had helper methods for partitioning the array in a quick sort, and an overloaded auxilary method allowing us to perform recursion. Any such methods are entirely up to you.
To hand in your program, copy SortTest.java
and any other files needed to create SortTest.class
to the following directory:
/p/course/cs367-ealexand/handin/login-name/P3
Use your actual CS login name (not your UW NetID!) in place of login-name.
Be sure that a comment at the top of the AVLTree.java
file gives the name(s) of the author(s) of the code.
If you worked with a partner, then only one of you should turn in the program files as specified above. It doesn't matter which partner hands in the program files.
Do not copy any ".class
" files, and do not create any
subdirectories in your handin
directory. Note that for this
assignment, you should test your program thoroughly, but you do not
need to hand in your test data.
You will be graded on program correctness, error checking, and general style.