ICS 211 Homework 4

Sorting

0. Goals and Overview

The goal of this assignment is to learn more about sorting.

This assignment requires you to implement a sorting algorithm that is a modified version of bubble sort.

1. Bubble Sort

Bubble Sort swaps pairs of adjacent elements repeatedly until the array is sorted. This is done in a regular fashion, sweeping through the array from left to right over and over again. Once no swap occurs during an entire sweep, the array is sorted.

Because Bubble Sort is well known and has many existing implementations, this assignment instead requires you to implement a variant which we will call DoubleBubble. In DoubleBubble, instead of swapping two adjacent elements, a reorder3 method looks at 3 adjacent elements and rearranges them in the array so they are in the correct order.

You may assume your array to be sorted has length at least 3.

2. Comparable (20%)

In this assignment you will sort the characters in an HW3String. To do so, you must change the header of HW3String.java to

public class HW3String implements java.lang.Comparable<HW3String> {
and add to HW3String a compareTo method:
  public int compareTo(HW3String other) {

This method must compare each character in this to each character in other, by computing the length to be compared (call it len) as the lesser of this.length and other.length, and looping over the first len characters of both strings as follows:

If you reach the end of the loop without returning, then

3. reorder3 (22%)

Create a reorder3 method with the following signature (method header):

public static boolean reorder3(HW3String[] a, int firstIndex);

After the call, the three values in a[firstIndex], a[firstIndex+1], and a[firstIndex+2], must be in sorted order.

The method returns true if the three values were already sorted, and false if the method had to rearrange them in the array.

There are many ways of implementing reorder3. Your implementation should be an extension of the swap method presented in class, and reproduced here:

public static boolean swap(HW3String[] a, int firstIndex) {
  if (a[firstIndex].compareTo(a[firstIndex + 1]) <= 0) {
    return true;   // already sorted
  }
  HW3String tmp = a[firstIndex];
  a[firstIndex] = a[firstIndex + 1];
  a[firstIndex + 1] = tmp;
  return false;
}

Hint: for reorder3 it may be easier to have 3 temporary variables rather than just 2, but it is possible to implement this method with just two temporary variables.

If firstIndex + 2 is greater than or equal to a.length, it is fine for your reorder3 method to throw java.lang.ArrayIndexOutOfBoundsException. If any code calls reorder3 with an index greater than or equal to a.length-2, this is not an error in reorder3, it is an error in the caller's code.

4. doubleBubble (20%)

Create a doubleBubble method with the following signature:

public static int doubleBubble(HW3String[] a);

This method must have an outer loop and an inner loop.

The inner loop calls reorder3 with every index from 0 to a.length - 3 inclusive (i.e. the loop bound is a.length - 2), and keeps track of whether any of the calls returns false.

The outer loop must be repeated until all the calls in the inner loop return true.

The method must keep track of (and return) how many times the outer loop executed the inner loop.

Hint: if implemented correctly, the value returned will never be less than 1, and never more than the length of the array.

5. Unit test (20%)

Your DoubleBubble class must have a meaningful unit test in its main method.

The unit test must sort at least 5 different arrays, each of length at least 8. The unit test must print each array both before and after sorting, and for each sort, must print the number of executions of the inner loop needed to sort the array.

6. Analysis (18%)

Analyze the execution time to give the O() runtime of (a) reorder3 and (b) doubleBubble, and (c) answer the question: is the O() of doubleBubble the same as the O() of bubble sort? (the wikipedia page gives the performance of the regular bubble sort).

For each of a, b, and c give three answers, (1) worst-case, (2) average case, and (3) best case. Here is a sample format for this answer (with x's where your answer should go):

a.1: O(x)    a.2: O(x)    a.3: O(x)
b.1: O(x)    b.2: O(x)    b.3: O(x)
c.1:  x      c.2:  x      c.3:  x

Each x on the last line should be either "same" or "not same".

Turning in the Assignment

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.