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.
Sorts
class contains the code we went over in class for Merge Sort, Quicksort, and Heap Sort. There's a Tester
class that creates arrays full of random values and calls sort methods, and an IntegerComparator
class that we'll use to help us count comparisons in Heap Sort and Java's built-in sort method.
compareTo
calls) performed during a sort. (That only gives us an estimate of the work done during the merge phase, but it should still help us establish the complexity.) I've already added code at the top of the Sorts
class that defines a counter variable and methods to clear it and to return its value. Add code to increment the counter wherever comparisons are occurring in the Merge Sort code.
Tester
, I've written code that creates an array of random values and sorts it. Add the necessary code to report how many comparisons were performed during a Merge Sort call.
mergeSort
again on the array we just sorted! Add some code that clears the counter, re-sorts the array, and reports how many comparisons were performed.
reverseArray
to reverse the now-sorted array. Sort that array and see how many comparisons it takes. You should leave all three of these tests in Tester
's main method, so that we see all three results on each run. Run it a few times to see if things change from run to run.
Tester
that reports the number of comparisons performed for the three test cases we did for Merge Sort: Sorting a random array, sorting an already sorted array, and sorting a reverse-sorted array. Which case(s) trigger its worst case behavior? What is its worst case behavior? How much variability is there from run to run?
partition
code by having it select a value from the array at random and use that as the pivot. See how many comparisons this modified Quicksort requires. (One way to do that is to swap the randomly chosen pivot value with the first value in the array, and then let the rest of the code run normally.) Note that you should not just use a random value as the pivot — you should use a value that's already in the list, but select it from a random position.
PriorityQueue
class as we insert and remove heap items, and we don't have access to that code. Luckily, PriorityQueue
will let us use a comparator object to determine the orderings, as we discussed in class.
I've provided an IntegerComparator
class that compares two Integer objects in the "standard" way. (The compare
method says that smaller Integers should come before larger ones.) It also maintains a counter so we can see how many times PriorityQueue
calls the compare
method. Add code to the Tester
class that creates an IntegerComparator
in addition to an array full of random values, and passes both to heapSort
. When the sorting's done, you can call getCount
on the comparator to see how many comparisons were done! Add code to try the other array orderings as well and print the results so we can compare it to the other two sorts.
Arrays
class, for example, has a static sort
method that sorts an array of Comparable
objects. The interactions below show this method being used to sort an array of five Integer
s:
> import java.util.Arrays; > Integer[] nums = {5, 4, 3, 2, 1}; > Arrays.sort(nums); > Arrays.toString(nums) "[1, 2, 3, 4, 5]" (String)
There's also a two-input version of sort
that takes a comparator object and uses that to determine the desired ordering. We can therefore use the same trick as you used in Heap Sort above to count the number of comparisons done in the built-in sort
.
IntegerComparator
class from your Heap Sort tests to investigate the number of comparisons done by the built-in sort method. Uncomment the code in Tester
that creates an IntegerComparator
and an array, and sorts it using the comparator object. Add code below the sort call that prints the number of comparisons performed. Run it and make sure the results are reasonable.
Arrays.sort()
is using?
long start = System.nanoTime(); // Do something that needs to be timed long end = System.nanoTime(); long elapsed = (end-start)/1000000; System.out.println("Took "+elapsed+" milliseconds");
IntegerComparator
to get other sorting orders. See if you can get the built-in sorting routine to sort the integers in reverse order, for example, or to sort based on the absolute value of the numbers so that negative values don't necessarily come before positive ones.