A08

Due Date: Thursday, March 21
File(s) to be submitted: InsertionSort.java, SortArray.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

Comparing Search Methods (Algorithmic Analysis)

Summary

Revise InsertionSort and SortArray so that we must provide a Comparator in order to sort the list.

You must not add any methods except compareTo in SortArray that takes the Comparator to use.

All other modifications must be to existing methods -- adding a Comparator parameter and using it.

Do not use raw generic types. (That is, every Comparator and SortArray must specify the bounds for its base type.)

The classes Person, Student and Undergrad are a small inheritance hierarchy, used to make sure your Comparators have appropriate bounds.

Details

The InsertionSort program I've provided you has been partially revised to sort using a Comparator. The method calls have been changed, but the method definitions have not.

Your task is to change the definitions of the following methods in InsertionSort:

Each of them must be revised to accept (and use) a suitable Comparator.

You must also add a new method to SortArray:

It's a variation on the existing method of the same name. The only difference is that it is given (and uses) a suitable Comparator.

Be sure to give every Comparator an appropriate base type. In some cases you will need to specify a restriction on the base type. See the insertionSort method and its restriction on the base type of the SortArray:

<T extends Comparable<? super T>>
That restriction says that the base type must implement Comparable, and that the Comparable it implements must be on some supertype of T. (For example, the Student class implements Comparable<Person>, where Person is a supertype of Student. It's the same for Undergrad -- likewise implementing Comparable<Person>.
It works for String, too, because String implements Comparable<String> and as far as super is concerned, every class is a supertype of itself.

So you need to think about what kind of a Comparator you need in order to sort a SortArray of base type T.


SUBMIT   /   Check