CMPU 102 - Assignment 7 Due: by 11:59 PM on Thursday, November 3rd

$Revision: 1.2 $

Sorting Algorithms

In this assignment you will implement several sorting algorithms.  Once you have implemented one of the algorithms, an animation will allow you to visualize how it works on random input.

The Sequence interface

You will be sorting an instance of the Sequence interface, which is defined as follows:

package edu.vassar.cs.cs102.sort;

public interface Sequence<E> {
    public int size();
    public void put(int index, E value);
    public E get(int index);
    public void swap(int index1, int index2);
}

The type parameter E represents the kind of element stored in the sequence.

The size method returns the number of elements in the sequence.  Like an array, the elements are indexed starting at 0.  The last element in the sequence is at index size() - 1.

The put method stores a value at a the given index in the sequence.

The get method retrieves the value of an element at the given index and returns it.

The swap method swaps the values of the elements located in the two locations referred to by index1 and index2.

The Sort interface

Your sorting algorithm implementations will be classes that implement the Sort interface, which is defined as follows:

package edu.vassar.cs.cs102.sort;

import java.util.Comparator;

public interface Sort<E> {
    public void sort(Sequence<E> sequence, Comparator<E> comparator);
}

The sort method sorts the given Sequence, using the given Comparator object to compare elements in the sequence.

The sorting algorithms

You should implement the following sorting algorithms:

The classes InsertionSort, BubbleSort, ShellSort, and QuickSort are provided for you, with empty sort methods in each for you to fill in with actual code.

Optional: you may choose implement Merge Sort instead of Quick Sort.  If you choose this option, then you should implement the sort method of the QuickSort class so that it performs a Merge Sort.  For temporary space, you may allocate an array whose element type is Object.  Note that when getting an element out of the array, you will need to cast it to the type parameter E before putting it in the Sequence object.
Note: if you do decide to implement the Quick Sort algorithm, you may use any pivot element, e.g., the first element in the subsequence.  (You don't have to use the "median of three" rule.)

The textbook describes each of these algorithms, with the exception of Bubble Sort.  Bubble Sort works as follows:

  1. On the first iteration, it goes through elements 0..n-1, swapping each out-of-sequence pair
  2. On the second iteration, it goes through elements 0..n-2, swapping each out-of-sequence pair
  3. etc...

This is called Bubble Sort because on each iteration, the largest value "bubbles" towards the end of the sequence.

Getting Started

Start by importing the file sortanim.zip into your Eclipse workspace.

You should start by implementing one of the easy algorithms: either Insertion Sort or Bubble Sort.  To test your algorithm, you can run the SortDemo program.  Just right-click on "SortDemo.java", and choose "Run As->Java Application".  You will see a window that looks like this:

The "Random" button randomly shuffles the input.  The button labeled "Large" or "Small" chooses between two input sizes.  Note that Insertion Sort and Bubble Sort will take a long time to complete on the Large input size.  The button labeled "Sort!" starts the sort algorithm selected by the combo box at the lower right.  (It will display "Running" while the sort algorithm is running.)  The text box with the counter displays how many operations (calls to get and put) the sorting algorithm has performed.

Testing

Make sure each of your sorting algorithms works for small random inputs.

Submitting

When you are done, run the following commands from a terminal window (pressing return after each one):

cd
cd eclipse-workspace
submit102 sortanim