Homework 4: Sorting 1
Due: Wednesday, Feb. 24, 11:59pm
In this homework, we will be using the LinkedList class we developed in Lab 4 for a purpose. (So this is the distinction between implementing a data structure and using or programming with a data structure.) The specific purpose will be sorting, and we'll see how often data structures and algorithms can go hand in hand.
Because implementing these sorting algorithms is very foundational and important in computer science, others have done it before. Please make sure to write only original code and refrain from looking at sources with implementation code. Doing so will allow you to have this valuable experience that no computer scientist should be without.
For this assignment you may work with a partner, using good pair programing practices (switch every 30 minutes, the non-keyboard partner is the main driver, telling the keyboard partner what to write). However, they must be a different person than Homework 2 (but can be the same as Lab 4).
To begin, download the following file and copy over your Node.java and LinkedList.java from Lab 4.
Insertion sort is a simple sorting algorithm where we build up the sorted list gradually. In this version of insertion sort, we'll first start out with an array. First we just take the first element of the unsorted array and create a list with just that element. Then we'll take the second element of the unsorted array and compare that with every element in the list, placing it in the correct position. We'll continue adding elements (at their correct position) until we've sorted the entire array.
@Override public String toString() { // TODO }
private static LinkedList insertionSort(String[] array) { // TODO }This will take in an array of Strings, but return a list of Strings, showing uses of these two data structures. It is very helpful to have a list for insertion sort since we are gradually adding elements at different positions. To compare two strings to see which comes first alphabetically, use the compareTo() method (investigate String Javadocs for more information about this method).
Within your sorting method, make sure to print out the list as it's being developed. So for this example, the output in this example should look like:
hi hi how are hi how are hi how you are hi how today you
Note that this dance implementation is in-place, but your implementation should use a linked list so as not to override the original array of elements.
Now we'll do the same thing again (for both examples), using bubble sort instead. Bubble sort starts by comparing the first two elements in the list, then swapping them if they are out of order. Then it moves on to the 2nd and 3rd elements, again swapping them if they are out of order. It continues through the list, ultimately resulting in the last element being in place (i.e. "bubbling" to the top). Then it starts all over again!
This algorithm can be done "in place" with the existing array, or it can be done by building a new list. Think about the merits and implementation issues of each approach, and then implement your choice as a new method in main. Make sure you get the same output with each type of sorting algorithm!
As a reminder, please be sure that all files are named exactly as described. Make sure you are using correct Javadoc comments for all fields and methods (and regular comments within methods). Include your name (@author field) and date (@version field) for each file. Starting from Homework 3 I will be evaluating on style.