## CSci 3501 Algorithms and Computability - Lab 2.

#### September 9. Due Monday, September 15 at 11:59pm

What to submit and when:

• All submissions are electronic: by e-mail to and CC to all lab partners. Please do not delete your e-mail from "Sent mail" or your mailbox until the end of the semester.
• When working on the lab, please comment your work so that it is clear what contributions of each person are.
• At the end of the lab each group should send me the results of their in-class work. Please indicate if this is your final submission.
• If your submission at the end of the lab time was not final, please send me(CC to the lab partner(s)) a final copy before the due time. Please use the subject "3501 Lab N", where N is the lab number.

### Lab assignment

Work in pairs.

#### Overview and goals

In this lab you will study the efficiency of mergesort and quicksort (see http://en.wikipedia.org/wiki/Quicksort) for different types of data (such as completely random, ordered, partially ordered). The efficiency of the algorithms is measured in the number of comparisons. Your program will call both sorting methods on the same data and measure the number of comparison operations performed by each sorting algorithm. The goal is to observe and analyze practical aspects of sorting. The lab also helps you learn testing methodology for algorithms analysis.

#### Technical details

You will use a predefined method sort that implements `mergesort`. Please read the method description carefully.

You will need to write your own implementation of `quicksort`. Your quicksort may take an array of `TestInteger` type (see below) according to the algorithm in the book. Use `compareTo` method to compare elements.

In order to count the number of comparisons you need to create your own class `TestInteger` that has an integer field `value` for comparison and a static `long` field `counter` to count the number of comparisons performed on all `TestInteger` objects. TestInteger must implement `Comparable` interface (or `Comparable<TestInteger>` if you are using generics).

The `compareTo` method of `TestInteger` must return the result of comparison of the values of the two `TestInteger` objects according to the specification in the Comparable interface: see compareTo. In addition it must increment the static counter by 1.

When generating arrays to be sorted, create two arrays and insert the same elements in both in the same order. Sort one array using `mergesort` and the other one using `quicksort`. Make sure to record and reset the `TestInteger` counter between the two sortings.

To make sure that quicksort works correctly, write a method `isSorted` to check if an array is sorted. When performing measurements, record the counter value before you run `isSorted` method since it would change the counter.

• Implement `TestInteger` and `quicksort` method as described above. Test your implementation.