Take a moment to introduce yourself to your partner(s). After social pleasantries are complete, pick one member of the team to be the "typer". They'll share their screen while editing the lab code in BlueJ. (I think BlueJ works better for these interactions than Eclipse. It's easier to see when sharing screens, and makes it easier to quickly test individual methods than Eclipse does.) Group members should contribute equally while working through the problems below and discuss all code to be written, though only the "typer" will be able to edit code. Resist the temptation to have both members work simultaneously in BlueJ — you're much more likely to "drift apart" over the course of the lab if you do so. The goal here is to have a partner who's engaged on exactly the same step of the lab as you are.
Sort
class, along with methods for accessing and clearing the counts.
selectionSort
so that comparisons
is incremented each time a comparison is performed between two data values in the array (regardless of its outcome), and swaps
is incremented each time a pair of items from the array exchange positions.
insertionSort
and bubbleSort
so that they also count the number of comparisons and swaps. In insertionSort
, consider it a "swap" any time a data value moves from one place to another within the array.
SortTester
class contains some code that will help: Methods for creating arrays of random ints, arrays of ints in increasing order, and arrays of ints in decreasing order. This will let you see whether the order of the values in the array influences the performance of the algorithms.
testSelectionSort
method in the SortTester
class. I've written code to generate three different arrays, sort each of them, and report the number of comparisons and swaps. (The output is a little cryptic — just numbers separated by tab characters — but that's intentional. Using tab characters means that we can copy and paste output from the terminal window into a spreadsheet and values will be in different cells.) In SortTester
's main method, write a for
loop that calls testSelectionSort
for sizes from 1000 to 20000, at increments of 1000.
testSelectionSort
and try to answer the questions below. It might be easier to figure out the scaling if you copy the data into a spreadsheet and have it draw a graph. (You could even have it fit a curve to the data if the pattern's not clear.)
testSelectionSort
and make some minor edits, then add some additional for loops in main to call these new routines with various problem sizes.
splitAndSort
in the Sorts
class. It works by splitting an input array into two equally sized pieces, sorting each piece with Selection Sort, and then merging the two sorted halves back together such that the result is ordered. For the last exercise I want you to examine its performance. (This is not Merge Sort, but we'll discuss this approach as a motivation for a faster sorting algorithm in class.)
splitAndSort
and its helper merge
. Make sure you understand what the methods are doing before you proceed. Then, add code as necessary so that the comparisons and swaps performed by splitAndSort
are being counted accurately. (Don't forget the comparisons that occur in merge
.)
SortTester
to measure the performance of splitAndSort
. How does it compare to plain old Selection Sort? Why?
splitAndSort
that splits into four pieces before sorting. You could keep splitAndSort
the way it is and use it as a helper for a new method that splits the input array, calls splitAndSort
on each half, then merges the results.
splitAndSort
so that instead of calling Selection Sort as a helper to sort each of the two smaller pieces, it recursively calls splitAndSort
instead! The required changes are pretty minimal...