Homework 7: Sorting, et. al.

This homework is intended to give you a chance to better understand sorting and searching.

A. Sorting Algorithms: Big Ideas

Here are some animations of selected sorting algorithms from the YouTube video I showed in lecture

Questions to ponder:

B. Implementing Sorting Algorithms

In the skeleton, MySortingAlgorithms.java provides a template for implementing the various sorting algorithms from 61B.

Try implementing the sorting algorithms without consulting your notes or the net (i.e., can you convert an understanding of the major idea behind an algorithm into code?). You don't have to do all of them, but at least do the starred ones

You can use MySortingAlgorithmsTest to test your implementations.

C. Timing Sorting Algorithms

The RunBenchmarks class runs timing tests on our various sorts. Currently, it is configured to perform two timing tests:

Run the code, and you should see results that are in line with things we've learned in class.

Other interesting tests you might try:

Feel free to post interesting tests and/or observations on Piazza.

D. Quicksort and Mergesort Mechanics (sortingProblems.txt)

Interestingly enough, quicksorting an array is equivalent to inserting all of its items into a BST. In this problem, we'll see why.

First, we need a specific implementation for partitioning.

Consider the array [5, 3, 2, 1, 7, 8, 4, 6], and suppose that we pick the leftmost item 5 as our pivot. One approach to partitioning is to perform a "stable" partitioning where all items that are less than 5 appear in the same order as they did before partitioning, and likewise for the greater items. For the given array, we'd get [3, 2, 1, 4, 5, 7, 8, 6].

One inefficient but simple way to implement this stable partitioning algorithm is to perform the following steps:

So for example if we partition [5, 3, 2, 1, 7, 8, 4, 6] from index 0 to index 7, we'd get:

Smaller items: [3, 2, 1, 4]
Equal items  : [5]
Larger items : [7, 8, 6]

The concatenation of these lists is just [3, 2, 1, 4, 5, 7, 8, 6]. Along the way, we compared the following pairs of numbers: 5-3, 5-2, 5-1, 5-4, 5-7, 5-8, and 5-6.

If we are using partition to sort, we then repeat this process for the left half and right half sides as discussed in class.

In sortingProblems.txt, fill out the list of comparisons used by Quicksort. You might find running Quicksort.java from the skeleton to be useful.

BSTs (1b, 1c, 1d in sortingProblems.txt)

Now draw the BST that results when you insert [5, 3, 2, 1, 7, 8, 4, 6] (in that order) into an initially empty BST. Record the comparions you observe in sortingProblems.txt. Answer questions 1c and 1d in sortingProblems.txt.

Mergesort (1e in sortingProblems.txt)

Finally, for 1e, give an example of a comparison performed by mergesort that is not performed by Quicksort or BST. This isn't particularly interesting, but we're just asking to make sure you understand how Mergesort works.

E. Sorting as a Tool

There are a large number of problems for which sorting provides a fast solution, even though the problem isn't really about sorting.

Define an interval to be a pair of numbers $[x_i, x_i']$ such that $x_i < x_i'$. An interval specifies a range of values in 1D space, where we can call $x_i$ the start point, and $x_i'$ the end point.

Given a list of such intervals, we want to know the total length of the regions covered by one or more of the intervals. This is not simply the sum of their lengths, $\Sigma x_i' - x_i$, since several may cover the same span.

For example, if we have intervals $[19, 30]$, $[6, 12]$, $[4, 5]$, $[8, 15]$, and $[3, 10]$, then the total length covered is 23: the last four intervals together totally cover the interval $[3, 15]$ of length 12, and the first covers a disjoint interval of length 11.

Fill in Intervals.java so that the coveredLength method returns the correct total length in $\Theta(N log N)$ time.

There is a clever trick to this problem that you'll need to figure out to get $\Theta(N log N)$ time. You do not need to use any nested loops for this problem, and in fact if you find yourself using them, you probably haven't found the right approach.

F. Various Problems

These problems are optional, but we encourage you to do them to get in good practice.

Distribution Count for Large Numbers (SortInts.java)

[Goodrich & Tamassia] Given a sequence of n distinct integers, each one of which is in the range $[0, n^2 - 1]$, develop an $O(n)$ algorithm for sorting them. See the skeleton file SortInts.java. You can't use ordinary distribution sort for this, because that would require initializing and traversing arrays of size $n^2$, which would take too long

Inversion Counting (Inversions.java)

Find an algorithm that runs in $O(n \lg n)$ time for computing the number of inversions in a list of n items. See the skeleton file Inversions.java. To test that your code is actually $O(n \lg n)$, provide it a very large list (say hundreds of thousands).

Two Sum (Sum.java)

[Goodrich&Tamassia] Given two sequences of integers, $A$ and $B$, find an algorithm that runs in $O(n \lg n)$ time (where n is the total number of integers in A and B) that determines, for a given parameter $m$, whether there is an integer $a$ in $A$ and an integer $b$ in $B$ such that $m = a + b$. See the skeleton file Sum.java. To test that your algorithm runs in $O(n \lg n)$ time, provide sequences of hundreds of thousands of integers. Feel free to use any of the methods in java.util.Arrays.

G. Balanced Search Trees

Put answers to the following problems in the file treeProblems.txt.

  1. Give a formula for the exact height of a binary search tree (as you implemented in homework 6) as a function of N if we insert N values in increasing order.
  2. Give a formula for the exact height of a 2-3-4 tree in terms of N if we insert N values in increasing order.

H. Submission

You will be required to submit:

  1. MySortingAlgorithms.java with Selection Sort, Insertion Sort, Quicksort, and LSD Radix Sort implemented
  2. sortingProblems.txt
  3. Intervals.java
  4. treeProblems.txt