CS 201 - Homework 4 - Sorting, Comparators, and Generics

Due: Wednesday 10/11 at 8am

This assignment has a written problem and a programming problem. When you are done, please submit your files (WrittenAnswers.pdf and MyVector.java) using the HW 4 submission page.


Lab exercises:

Starting with the programming problem on this homework (problem 2 below), you'll be using Eclipse to develop your code. Eclipse is an IDE (integrated develpoment environment) that manages your files, compiles "on the fly", and will make it much easier to run and debug your code. During lab, we will help you install Eclipse on your own computers, and walk you through the following steps:

Start by downloading hw4.zip, and extract this archive in your cs201 directory. This will create a new hw4 directory there:

> cd cs201
[download hw4.zip and save it here]
> unzip hw4.zip
> cd hw4
> ls
AbsoluteIntegerComparator.java  MyVector.java                  TestMyVector.java
CaselessComparator.java         ReverseIntegerComparator.java
IntegerComparator.java          StringComparator.java
Next, proceed as follows: Next, in the "Package Explorer" (or "Project Explorer"), expand "hw4" and within that expand "(default package)". Double-click on any of the java files to open them. Once you have a runnable application (for this homework it would be "TestMyVector.java") that does not have any errors, click on the green arrow button at the top to run it.

You are now ready to get started on problem 2 below. Additional instructions will be provided in lab.


Problem 1 (written problem): Identifying Sorting Algorithms

When working for AsSorted Systems as a summer intern, Bud Lojack (correctly) implemented four sorting algorithms for arrays: selection sort, insertion sort, merge sort, and quick sort. But instead of giving meaningful names to his routines, Bud named them sort1, sort2, sort3, and sort4. When writing up a report at the end of the summer, Bud needs to figure out which routine corresponds to which algorithm. Unfortunately, Bud has accidentally deleted the source files for his routines and cannot even inspect the code to determine which routine implements which algorithm. The only thing he can do is test the routines on various inputs. Below are his timing results for running the routines on input arrays of various sizes. For each routine, he tested the routine on both already sorted arrays and on randomly ordered arrays.

Time for sort1 to sort sorted array with 400 elements:         4
Time for sort1 to sort sorted array with 800 elements:         8
Time for sort1 to sort sorted array with 1600 elements:       16
Time for sort1 to sort sorted array with 3200 elements:       26
 
Time for sort1 to sort random array with 400 elements:       255
Time for sort1 to sort random array with 800 elements:       958
Time for sort1 to sort random array with 1600 elements:     4059
Time for sort1 to sort random array with 3200 elements:    16585
 
Time for sort2 to sort sorted array with 400 elements:        92
Time for sort2 to sort sorted array with 800 elements:       339
Time for sort2 to sort sorted array with 1600 elements:     1356
Time for sort2 to sort sorted array with 3200 elements:     5321
 
Time for sort2 to sort random array with 400 elements:        13
Time for sort2 to sort random array with 800 elements:        37
Time for sort2 to sort random array with 1600 elements:       71
Time for sort2 to sort random array with 3200 elements:      143
 
Time for sort3 to sort sorted array with 400 elements:       229
Time for sort3 to sort sorted array with 800 elements:       907
Time for sort3 to sort sorted array with 1600 elements:     3311
Time for sort3 to sort sorted array with 3200 elements:    13634
 
Time for sort3 to sort random array with 400 elements:       205
Time for sort3 to sort random array with 800 elements:       890
Time for sort3 to sort random array with 1600 elements:     3318
Time for sort3 to sort random array with 3200 elements:    13689
 
Time for sort4 to sort sorted array with 400 elements:        23
Time for sort4 to sort sorted array with 800 elements:        37
Time for sort4 to sort sorted array with 1600 elements:       87
Time for sort4 to sort sorted array with 3200 elements:      178
 
Time for sort4 to sort random array with 400 elements:        24
Time for sort4 to sort random array with 800 elements:        51
Time for sort4 to sort random array with 1600 elements:      102
Time for sort4 to sort random array with 3200 elements:      213

Part a. Based on Bud's data, create a table like the one below, and fill in the asymptotic notation that best describes the running time of the routine on a particular type of array. You should use one of the following for every entry of the table: O(1), O(log(n)), O(n), O(nlog(n)), O(n2), O(n3). (Note: because the numbers are taken from an actual implementation, they may not fit any category exactly; if the category is ambiguous, say so!)

Sorted Array

Unsorted Array
sort1  
sort2  
sort3  
sort4  

Part b. Based on the table from part a, determine for each of Bud's four routines, which of the four sorting algorithms it uses. Briefly explain your reasoning.

Part c. Answer the following for the four sorting algorithms that Bud has implemented:

i. Which sorting algorithm(s) does much better on sorted arrays than on random arrays? Why?

ii. Which sorting algorithm(s) does much worse on sorted arrays than on random arrays? Why?

iii. Which sorting algorithm(s) takes about the same amount of time on both sorted and random arrays? Why?

You can use the template WrittenAnswers.doc as a starting point to type in your answers. When you are done, please save this file in PDF format and upload the resulting file WrittenAnswers.pdf via the HW submission page.


Problem 2: Implementing Sorting Algorithms Using Vectors

This problem is similar to the lab problem "Sorting with Comparators" in Section 6.11 of the Bailey text. The question in the text asks you to add a single method sort. I would like you to implement three sorting algorithms, selectionSort, insertionSort, and quickSort. On the other hand, I am also providing testing code, so you won't have to write that.

For reference, the code for five sorting algorithms we examined in class is given in SortArrays.java.

In this assignment you will be extending the Vector class to add the ability to sort. As the exercise describes, write a new generic class called MyVector<T> that extends Vector<T> and adds the ability to sort using selectionSort, insertionSort, and quickSort. Since a vector can store general objects, this involves also specifying a Comparator that compares two elements A and B of the vector and returns a value less than 0, equal to 0, or greater than 0, if A<B, A==B, or A>B, respectively (see Bailey 6.7-9 for reference). You will then write three new methods for the MyVector class:

public void selectionSort(Comparator<T> c) 
public void insertionSort(Comparator<T> c) 
public void quickSort(Comparator<T> c)

Start by download the file hw4.zip and unzipping it in your cs201 folder.

Then, import the hw4 project into Eclipse, as described in the lab exercises above. Besides MyVector.java, which you will have to complete, this includes a file TestMyVector.java that tests your class using five simple implementations of Comparator, which are also included. Study all the given code well. Compile TestMyVector.java first; this will result in error messages that show you what you have to implement.