In this lab, we will implement multiple sorting algorithms and compare their perfomance.
sorting.gui
Take a look at the code inside GnomeSorter.java. This is an example algorithm demonstrating elements that you should use in your implementations below. Each algorithm below will need to implement the sortAlgorithm method, which brings in an ArrayList to be sorted.
GnomeSorter.java
sortAlgorithm
ArrayList
First, we can access the elements in the list with the get method, and determine its size with the size method. However, notice that we do not use the set element of the list. Instead, we call our own set method, which takes the list to be set, the index that will be set, and the element to set in the index location. This roundabout method is used to assist with the animation.
get
size
set
Next, since we have an ArrayList of generic elements, we need to call the compareTo method. This will return an integer, equal to 0 if the two elements are the same, negative if first is smaller than the second, and positive if the first is larger than the second, according to whatever ordering scheme is defined. We will want our resulting array to be sorted from smallest to largest.
compareTo
swap
Bubble sort is known for its simplicity of code. Repeated passes through the data quickly push the largest elements to the end, and slowly drag the smallest elements to the front of the list.
Create a new class called BubbleSorter. To fit into the Sortimator hierarchy, it will need to extend the generic Sorter class. Also, the generic type E will need to extend the Comparable interface. The name of your class should be
BubbleSorter
Sorter
E
Comparable
BubbleSorter<E extends Comparable<E>> extends Sorter<E>
BubbleSort can be implemented with the following algorithm.
To save time, each scan can reduce the elements it examines by one, since on the first pass, we can guarantee that the highest element will be in the right location, and on the second pass, the second-highest element will be in the right location, etc.
Run your code through the SorterTester suite to make sure your implementation has the correct behavior.
SorterTester
MergeSort uses recursion to repeatedly split the given list into smaller lists, sort the smaller lists, and then combine the sorted sublists into one sorted list.
The name of your class should be
MergeSorter<E extends Comparable<E>> extends Sorter<E>
First, you will need to create a void mergeSortHelper(ArrayList<E> array, int start, int end) method. In order to do recursion, we will need to track the start and end indices of our subarrays. The start and end should be additional parameters along with the list. Use end as we have in other contexts, to be the stopping index, going up to but not including this index.
void mergeSortHelper(ArrayList<E> array, int start, int end)
start
end
So, our sortAlgorithm will call the mergeSortHelper method with start as 0 and end as the size of the list.
mergeSortHelper
mergeSortHelper has the following structure:
midpoint
To complete this method, we need a void merge(ArrayList<E> array, int start, int end) method. The most straightforward implementation involves using a Queue. In Java 21, you should use the ArrayDeque a double-ended queue which can be found in the java.util package:
void merge(ArrayList<E> array, int start, int end)
java.util
add()
element()
remove()
merge
Whereas MergeSort was an easy journey down the recursion but complicated merging back up, QuickSort reverse this scheme. Before recursing, QuickSort partitions the elements of the list, hopefully into two equal-sized portions, placing the elements smaller than a randomly chosen pivot element to the left and those elements larger to the right. These subarrays will be semi-sorted, and then repeatedly partitioned until all elements are in the correct order.
QuickSorter<E extends Comparable<E>> extends Sorter<E>
Again, we will need a recursive helper function, augmenting with the start and end of the subarray. void quickSortHelper(ArrayList<E> array, int start, int end) has the following structure:
void quickSortHelper(ArrayList<E> array, int start, int end)
quickSortHelper
The int partition(ArrayList<E> array, int start, int end) method should have the same parameters as the quickSortHelper method.
int partition(ArrayList<E> array, int start, int end)
pivot
Describe in your own words the strengths and weaknesses of each of the three implementations above. Use the Sortimator class to run each algorithm 3 times, on a list of size 20. Record the number of Array Updates that each method executes, as found through the GUI.
Sortimator