A08

Due Date: Thursday, November 19
File(s) to be submitted: Shaker.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

More Improvements on BubbleSort

Summary

This week you will program an improved version of bubble sort that's called shaker sort. This version sorts arrays of int values.

Details

In L07 we made an improvement to bubble sort. The idea was to notice where the last swap occurred and update the loop bounds accordingly.

That improved version of bubble sort, however, would give no benefit when sorting this array:

A mostly sorted list: 10 2 3 4 5 6 7 8 9 1
In this array the 1 only ever moves down one position, and so the method must do all nine iterations:
2 3 4 5 6 7 8 9 1 10 2 3 4 5 6 7 8 1 9 10 2 3 4 5 6 7 1 8 9 10 2 3 4 5 6 1 7 8 9 10 2 3 4 5 1 6 7 8 9 10 2 3 4 1 5 6 7 8 9 10 2 3 1 4 5 6 7 8 9 10 2 1 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

Shaker sort aims to improve the performance by bubbling small items toward the front as well as large items toward the rear. The method alternates between pushing a large value up and pushing a small value down:

10 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 10 1 2 3 4 5 6 7 8 9 10

In the first pass the 10 is pushed to the end of the list. In the second pass the 1 is pushed up to the front. At this point the array is sorted (tho' shaker sort needs to take one more pass thru in order to notice that). Consider, for example, the list we sorted in L07. Shaker sort's first two passes (one up, one down) would give this:

A mostly sorted list: 3 4 5 1 2 6 7 8 9 10 ...press enter... Bubbling UP: Unsorted part 3 4 1 2 5 6 7 8 9 ... Sorted part ... ... ... ... ... ... ... ... ... 10 Bubbling DOWN: Unsorted part ... 3 4 2 5 6 7 8 9 ... Sorted part 1 ... ... ... ... ... ... ... ... 10 ...press enter...

Noter that the unsorted part truly is unsorted, and shaker sort will need two more passes (one up, one down) to make it sorted.

Bubbling UP: Unsorted part ... 3 2 ... ... ... ... ... ... ... Sorted part 1 ... ... 4 5 6 7 8 9 10 Bubbling DOWN: Unsorted part ... ... 3 ... ... ... ... ... ... ... Sorted part 1 2 ... 4 5 6 7 8 9 10 ...press enter...

At this point shaker sort can deduce that the array is sorted without doing any more passes: the "unsorted" part is only one space long, and so it must actually be sorted (no list of length one can be unsorted).

So here is how shaker sort works:

Notes on begin and end

When tracking the beginning and end of (part of) an array, we don't usually point to its first and last elements:

First  Second  Third  Last
 104     -6      1     42
The first element is at index 0; the last element is at index 3.

Instead we track the first element and the one after the last element:

Begin                       End
 104     -6      1     42
The beginning is at index 0; the ending is at "index" 4.

If the whole array is being included, then the beginning is 0 and the ending is the array's length. You can see an example of this in the code for Common::printArray:

public static void printArray(int[] arr) { printArray(arr, 0, arr.length); }
To print out the whole array, we print out the parts from index 0 to the length of the array.

For shakerSort, you want to track the beginning and end of the (possibly) unsorted part of the array. Thus for this unsorted part:

Unsorted part ... 3 2 ... ... ... ... ... ... ...
the beginning is 1 and the ending is 3. The length of the unsorted part is 3 - 1 = 2.

I have given you my BubbleSort program (renamed to Shaker) as a model for the program I want. It produces output similar to what's shown above (and like what's shown in the sample output). The difference being that Common doesn't have a method for printing the front and back of an array, so we'll just print the full array along with the unsorted part:

Array after bubbling up: 3 4 1 2 5 6 7 8 9 10 Unsorted part 3 4 1 2 ... ... ... ... ... ... ...press enter... Array after bubbling down: 1 3 4 2 5 6 7 8 9 10 Unsorted part ... 3 4 2 ... ... ... ... ... ... ...press enter...

Make sure that your output is exactly the same as the sample output for the first two arrays. Of course, the random array will show different output each time, and thus will be different from what's shown in the sample output.

Note that for the second array (the totally sorted array), shaker sort makes a pass bubbling down even tho' the array was noted as being totally sorted after the pass bubbling up:

An entirely sorted array: 1 2 3 4 5 6 7 8 9 10 ...press enter... Array after bubbling up: 1 2 3 4 5 6 7 8 9 10 Unsorted part ... ... ... ... ... ... ... ... ... ... ...press enter... Array after bubbling down: 1 2 3 4 5 6 7 8 9 10 Unsorted part ... ... ... ... ... ... ... ... ... ... ...press enter...

It's not a problem because the pass going down shouldn't do any comparisons. It should start at the top of the unsorted portion of the array, and that's right at the beginning of the array. So really it's reporting a pass bubbling down, but it's not actually doing any work for it.


SUBMIT   /   Check