CMSC 351 Homework 6: Quicksort and Carnival Game Problems - Prof. Clyde P. Kruskal, Assignments of Algorithms and Programming

Five problems from a computer science course, including analyzing the quicksort algorithm, determining the expected gain, variance, and standard deviation of a carnival game, and finding a constant for a recursive integer multiplication algorithm.

Typology: Assignments

Pre 2010

Uploaded on 07/29/2009

koofers-user-acw
koofers-user-acw 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Summer 2008 CMSC 351: Homework 6 Clyde Kruskal
Due at the start of class Wednesday, June 25, 2008.
Problem 1. Consider an array of size eight with the numbers 50,70,10,20,60,40,80,30.
Assume you execute quicksort using the version of partition from CLRS.
(a) What is the array after the first partition. How many comparisons did you use?
How many exchanges?
(b) Show the left side after the next partition. How many comparisons did you use?
How many exchanges?
(c) Show the right side after the next partition on that side. How many comparisons
did you use? How many exchanges?
(d) What is the total number of comparisons in the entire algorithm? What is the
total number of exchanges in the entire algorithm?
Problem 2. Assume you execute quicksort using the version of partition from CLRS.
(a) What is the fewest exchanges that the algorithm will execute for an input of size
n.
(b) Give an example with n= 8.
Problem 3. Write an algorithm Partition(A,p,r,s) to partition array Afrom pto r
based on element A[s], using exactly rpcomparisons and rp+O(1) moves. The
element A[s] should end up in its proper sorted location after partitioning.
Problem 4. A carnival game consists of three dice in a cage. A player can bet a dollar on
any of the numbers 1 through 6. The cage is shaken, and the payoff is as follows. If
the player’s number doesn’t appear on any of the dice, he loses his dollar. Otherwise
if his number appears on exactly kof the three dice, for k= 1,2,3, he keeps his dollar
and wins kmore dollars.
(a) What is his expected gain from playing the carnival game once?
(b) What is the variance from playing the carnival game once?
(c) What is the standard deviation from playing the carnival game once?
(d) What are his expected gain, variance, and standard deviation from playing the
carnival game ntimes?
Problem 5. Challenge problem. Consider the following recurrence that basically occurs
from one of the recursive integer multiplication algorithms:
T(n) = (4T(n/2) + nif n > 1
0 if n= 1
Find at constant asuch that T(n) = an2+O(n). Justify your answer.

Partial preview of the text

Download CMSC 351 Homework 6: Quicksort and Carnival Game Problems - Prof. Clyde P. Kruskal and more Assignments Algorithms and Programming in PDF only on Docsity!

Summer 2008 CMSC 351: Homework 6 Clyde Kruskal

Due at the start of class Wednesday, June 25, 2008.

Problem 1. Consider an array of size eight with the numbers 50, 70 , 10 , 20 , 60 , 40 , 80 , 30. Assume you execute quicksort using the version of partition from CLRS.

(a) What is the array after the first partition. How many comparisons did you use? How many exchanges? (b) Show the left side after the next partition. How many comparisons did you use? How many exchanges? (c) Show the right side after the next partition on that side. How many comparisons did you use? How many exchanges? (d) What is the total number of comparisons in the entire algorithm? What is the total number of exchanges in the entire algorithm?

Problem 2. Assume you execute quicksort using the version of partition from CLRS.

(a) What is the fewest exchanges that the algorithm will execute for an input of size n. (b) Give an example with n = 8.

Problem 3. Write an algorithm Partition(A,p,r,s) to partition array A from p to r based on element A[s], using exactly r − p comparisons and r − p + O(1) moves. The element A[s] should end up in its proper sorted location after partitioning.

Problem 4. A carnival game consists of three dice in a cage. A player can bet a dollar on any of the numbers 1 through 6. The cage is shaken, and the payoff is as follows. If the player’s number doesn’t appear on any of the dice, he loses his dollar. Otherwise if his number appears on exactly k of the three dice, for k = 1, 2 , 3, he keeps his dollar and wins k more dollars.

(a) What is his expected gain from playing the carnival game once? (b) What is the variance from playing the carnival game once? (c) What is the standard deviation from playing the carnival game once? (d) What are his expected gain, variance, and standard deviation from playing the carnival game n times?

Problem 5. Challenge problem. Consider the following recurrence that basically occurs from one of the recursive integer multiplication algorithms:

T (n) =

{ 4 T (⌈n/ 2 ⌉) + n if n > 1 0 if n = 1

Find at constant a such that T (n) = an^2 + O(n). Justify your answer.