CSPP 55001 Algorithms — Autumn 2011

Homework 2 (assigned October 6, due October 12; Revised)

Reading: CLRS chapters 6 and 7. Reading for next week's lecture: CLRS chapter 15, sections 15.1–15.4.

Written assignment: Solve the following "Do" exercises and assigned problems. Only solutions to the assigned problems should be turned in.
Note: You are responsible for the material covered in both "Do" exercises and assigned problems.
Note: If you work with others, indicate their names at the top of your homework paper. Everyone must submit their own independently written solutions.

"Do" Exercises (not to be handed in):

  1. Problem 7-1, parts a–e on pages 185–186.

  2. Problem 7-2, parts a–d on pages 188–189.

  3. Exercise 6.2-1 and 6.2-2 on page 156.

  4. Exercise 6.5-1 and 6.5-2 on pages 164–165.

Assigned Problems (to be handed in):

Important: When describing an algorithm in pseudocode, explain the meaning of your variables in English. Comment the lines of your pseudocode. Your pseudocode should be short, clear, and complete to receive full credit. If your code is long, difficult to read, or incomplete, you will not receive full credit.

  1. We can improve the running time of quicksort in practice by taking advantage of the fast running time of insertion sort when its input is "nearly" sorted. Upon calling quicksort on a subarray of fewer than k elements, let it simply return without sorting the subarray. After the top-level call to quicksort returns, run insertion sort on the entire array to finish the sorting process.

  2. Suppose an array A consists of n elements, each of which is green, white, or orange. We wish to sort the elements so that all the greens come before all the whites, which come before all the oranges. The only operations permitted on the keys are the following.
    examine(A, i): report the color of the ith element of A.
    exchange(A, i, j): exchange the ith element of A with the jth element.
    Give a correct O(n) algorithm for green-white-orange sorting. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. No credit will be given for algorithms that are not O(n).

  3. There are two principal alteratives for constructing a max-heap from a given set of keys. The first is a bottom-up construction algorithm given by the following pseudocode (cf. page 157):
    BUILD-MAX-HEAP(A[1..n]))
    01 heapsize[A] = n
    02 for i = floor(n/2) downto 1
    03         MAX-HEAPIFY(A, i)
    The alternative algorithm constructs a max-heap by repeatedly calling MAX-HEAP-INSERT to insert the elements into the heap. It is given by the following pseudocode:
    BUILD-MAX-HEAP′(A[1..n]))
    01 heapsize[A] = 1
    02 for i = 2 to n
    03         MAX-HEAP-INSERT(A, A[i])

  4. Let A be a min-heap and i the index of a node in A. We change the key of the element A[i] to newkey, where the new value is less than the old value.

  5. In class we considered binary heaps, where each node has at most two children. A d-ary heap is a heap in which each nonleaf node (except perhaps one) has exactly d children.


Gerry Brady
Thursday October 6 11:27:03 CDT 2011