# CS5800 09F: Homework 03

Created: Tue 22 Sep 2009

Assigned: Wed 23 Sep 2009
Due: Wed 30 Sep 2009

## Instructions

1. Please review the course syllabus and make sure that you understand the course policies for grading, late homework, and academic honesty.

2. On the first page of your solution write-up, you must make explicit which problems are to be graded for "regular credit", which problems are to be graded for "extra credit", and which problems you did not attempt. Please use a table something like the following

Problem01020304 0506070809...
CreditRCRCRCECRC RCNARCRC...

where "RC" is "regular credit", "EC" is "extra credit", and "NA" is "not applicable" (not attempted). Failure to do so will result in an arbitrary set of problems being graded for regular credit, no problems being graded for extra credit, and a five percent penalty assessment.

3. You must also write down with whom you worked on the assignment. If this changes from problem to problem, then you should write down this information separately with each problem.

## Problems

Required: Do all four exercises (1 through 4) and two of the three problems (5, 6, 7).
Points: 15 pts per exercise and 20 points per problem.
Unless otherwise indicated, exercises and problems are from Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein. The edition (2nd or 3rd) will be indicated if the numbering differs.

1. Exercise 6.5-7 (CLRS3) or 6.5-6 (CLRS2)
2. Exercise 6.5-8 (CLRS3) or 6.5-7 (CLRS2) (Prove that your Heap-Delete(A, i) runs in O(lg n) time.
3. Exercise 6.5-9 (CLRS3) or 6.5-8 (CLRS2) (Prove that your algorithm works in O(n lg k) time.

5. Problem 6-3
6. Consider the following "3-way" sorting algorithm.

ThreeWaySort(A, i, j)

1. if A[i] > A[j]
2.      exchange A[i] ↔ A[j]
3. if i + 1 ≥ j
4.      return
5. k ← floor(j - i + 1)/3)               ⁄⁄ Round down.
6. ThreeWaySort(A, i, j - k)       ⁄⁄ First two-thirds.
7. ThreeWaySort(A, i + k, j)      ⁄⁄ Last two-thirds.
8. ThreeWaySort(A, i, j - k)       ⁄⁄ First two-thirds again.

(a) Argue that ThreeWaySort(A, 1, n) correctly sorts the input array A[1 .. n].

(b) Give a recurrence for the worst-case running-time of ThreeWaySort and a tight asymptotic (Θ-notation) bound on the worst-case running time.

(c) Compare the worst-case running time of ThreeWaySort with that of insertion sort and mergesort.

7. A mode of an array A[1 .. n] is an element of A that occurs the most number of times in A. Thus, a mode of the array A = [1,7,4,12,4,1,1,4] is 4, which occurs three times. The entry 1 is also a mode of A.

(a) Given an array A[1 .. n] of n integers, show how a mode of A can be determined in O(n log n) time.

An array A[1 .. n] is said to have a majority element if more than half of its entries are the same. We would like to determine whether a given array A has a majority element, and if so, find the element. Unlike in part (a), however, we will not restrict the elements to be integers. In fact, we assume that the elements of the array are not necessarily from some ordered domain like the integers, so there can be no comparisons of the form "is A[i] > A[j]?''; only questions of the form "is A[i] = A[j]?" can be answered.

(b) Show how to solve the majority element problem in O(n log n) time.

Hint: Split the array into two arrays A1 and A2 of half the size. Use a divide-and-conquer approach that finds the majority element of A, if it exists, using the knowledge of majority elements of A1 and A2, if they exist.)

(c) Can you give a linear-time algorithm?

Hint: Another divide-and-conquer approach is as follows:
(a) pair up the elements of A arbitrarily, to get n/2 pairs;
(b) if two elements of a pair are different, then discard both of them, else keep just one of them. Show that after this procedure there are at most n/2 elements left, and that they have a majority element if A does.

### Switch to:

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #340 WVH,
Boston, MA 02115
Email: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/CS5800/F09/Homeworks/hw.03.html 