


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CS231 Algorithms Handout # 14 Prof. Lyn Turbak October 16, 2001 Wellesley College
Reading: Handouts 12 (Array Sorting) and 15 (Linear Sorting); CLRS Chapters 7 and 8; CLR Chapters 8 and 9.
Sugggested Problems: CLRS 8.1-1, 8.1-2; 8.2-3, 8.3-2; 8.3-3 (CLR 9.1-1, 9.1-2; 9-2.3; 9.3-2, 9.3-3).
Required Problems: You should write up and turn in the solutions to the problems listed below: The points awarded per problem are given in brackets.
Problem 1 [33]: The Dutch National Flag Problem (The following problem was proposed by W.H.J. Feijen and made famous by Edsger W. Dijkstra, both of Dutch origin.) You are given a row of n buckets, each containing one ball, which may be either red, white, or blue. Your goal is to rearrange the balls in the buckets such that they appear in the order of the colors on the Dutch national flag: red balls should be grouped on the left, white balls in the middle, and blue balls on the right. The only operation you may use to move balls is swap(i, j), which swaps the contents of the ith and jth buckets.
a. [15] Give pseudocode for a linear-time algorithm for sorting an array B[1..n] of balls in the Dutch national flag order. Your algorithm should use only constant space in addition to the given array. Hint: study the Lomuto partitioning technique for quicksort from Handout 12 and CLRS 7.1 (unfortunately, CLR only covers Lomuto in problem 8-2).
b. [15] Prove that your algorithm is correct by the method of loop invariants. Recall that this requires the following steps:
c. [3] According to the decision-tree analysis performed in class (and in CLRS 8.1/CLR 9.1), the best worst-case running time of a comparison-based sorting algorithm is Θ(n · lg n). Yet the Dutch national flag problem is a linear-time sorting algorithm. Explain how this can be.
Problem 2 [27]: Stooge Sort Do CLRS Problem 7-3 (CLR Problem 8-3). For part a, give a proof of correctness by induction on the number of elements n = (j − i + 1) in the array. Recall the structure of an induction proof for the correctness of an algorithm:
The correctness proof is somewhat subtle, so please explain it carefully. For simplicity, you may assume that the array segment A[i..j] is non-empty and that all elements in this array segment are distinct. As part of your proof, you should carefully show that the arrays A[i..j-k] and A[i+k..j] men- tioned in lines 6–8 have lengths less than n. You should also carefully show the bounds (as a function of n) on the sizes of the three array segments A[i..i+k-1], A[i+k..j-k], and A[j-k+1..j].
Hint: For each element in the original segment A[i..j], define its fate as one of the following three labels:
Use these fate labels to guide your correctness proof.
Grading breakdown on parts of this problem:
Problem 3 [20] Do CLRS 8.2-4 (CLR Exercise 9.2-5). Note that the notation [a..b] means the range of integers from a to b, inclusive.
Problem 4 [20] Do CLRS Exercise 8.3-4 (CLR Exercise 9.3-4). Hint: View an integer x in the range [1..n^2 ] as a pair of ”digits” (x div n, x mod n) (where x div n finds the integer quotient of x divided by n, and x mod n finds the integer remainder of x divided by n).
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.