The goal of this assignment is to learn more about sorting, specifically the selection sort, insertion sort, and bubble sort algorithms.
Sorting is the process of arranging data in a collection (in our case an array) so that it is ordered, usually from smallest to largest or from first to last. For example, given an array that has the three strings "i", "c", and "s", sorting the array would place the three strings in the order "c", "i", and "s". An array with the numbers 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 (the first 10 digits of pi) would be sorted to have the numbers in the order 1, 1, 2, 3, 3, 4, 5, 5, 6, 9.
Sorting can also be modified to only give unique elements, that is, eliminate duplicates. For example, sorting 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 while only keeping unique elements gives 1, 2, 3, 4, 5, 6, 9. More interestingly, sorting the characters of the string
"the quick brown fox jumps over the lazy dog"gives the string
" abcdeeefghhijklmnoooopqrrsttuuvwxyz"(note the 8 blank characters sorting before all of the letters).
In contrast to this normal sorting, sorting this particular string (which includes all the letters of the alphabet) while keeping only the unique characters gives a single blank character followed by the complete alphabet:
" abcdefghijklmnopqrstuvwxyz"
For another example, sorting "hello world" gives " dehllloorw", but when only keeping unique characters, the result is " dehlorw".
In this assignment you must implement selection sort, insertion sort, and bubble sort on arrays of characters, and also a variant of insertion sort that eliminates duplicates and only retains one each of the characters in a string.
Since our sorting algorithms sort in place, and we cannot conveniently replace individual characters in an existing string, your implementation of the sorting algorithms must sort arrays of characters.
Given a string s, s.toCharArray() returns a new array of characters containing all the characters in the string, in order. So for example if s = "edo", s.toCharArray() returns a 3-element array where the first element is 'e', the second element is 'd', and the third element is 'o' (characters in Java are surrounded by single quotes, Strings by double quotes).
Given an array a of characters, new String(a) creates a string with all the characters in the array, in order.
The instructor has provided a program, hw04.java, that makes it easy to test sorting algorithms. You must download this program, fill in the parts marked student must implement, and turn it in as your answer to the assignment.
hw04.java makes it easy to test your sorting algorithms on different strings by providing different command-line arguments. You are strongly encouraged to test your working code on different strings, including at the very least your own name(s) and at least a few long strings.
Be sure to add your own name to the list of authors for the code you turn in.
Before implementing any sorting algorithm, you must implement the swap method, which exchanges (swaps) the values in the array a stored at indices i1 and i2. You are welcome to use the code presented in lecture. Please make sure you thoroughly understand the code.
In this part, you implement the standard sorting algorithms described in Sections 8.2 and 8.3 of the textbook, and (especially for bubble sort) in class. In doing so, you may take inspiration from code presented in the book or elsewhere, as long as you completely understand and write comments to document the code you turn in.
Such documentation is especially important for Insertion Sort, but must be provided for all three sorts and for the code in section 6.
Your TA may review your comments to verify your understanding of the code you turn in, and may deduct points if the comments do not clearly show understanding or are not your own.
All three algorithms could be modified to only keep unique values, but for this assignment you will only modify insertion sort. When the original array has duplicates, the sorted array will have fewer elements than the unsorted array.
As described in Section 1, for this part of the assignment keeping only unique values keeps exactly one of each character in the string. For example, such a sort on the string "hello" would return "ehlo".
There are at least two ways to modify Insertion Sort to remove duplicates:
You may implement any one of these algorithms, or come up with your own, as long as it implements insertion sort and works correctly.
If your string had duplicate characters, you must then shrink the array by calling Arrays.copyOf. To do this correctly, you need to keep track of the number of duplicates.
Once you have your array of sorted unique characters, turn the array into a String and return the String.
You again must provide clear comments to explain what your code is doing.
Use Laulima to turn in your files to the TA. Once you log into Laulima and select the ICS 211 site, on the left-hand side will be an assignments tab. Double-check the due date to make sure you are submitting to the correct current assignment.