Unformatted Attachment Preview
CS141 Midterm1 Part 1
Problem 1: Let A be an unsorted array of n positive integers. Recall linear time selection algorithm.
of finding a pivot element by computing the median of medians, let a pivot be the element
with index n2 . The rest of the algorithm is the same as above. Will the algorithm still be correct? If yes,
justify and give its running time. If not, show why.
b) Suppose, you decided to find a pivot element by partitioning the given array of integers into groups
of 11 (instead of 5). What recurrence would give the worst-case running time of your algorithm? Give a
Problem 2: Let A be an (unsorted) array of n distinct integers. You want to partition this array into k
subarrays A1 , A2 , ..., Ak (where k is a small integer) in such a way, that any element that belongs to Ai is
less than any element that belongs to Aj if i < j. You need to design an O(n) algorithm. You can use any
of the algorithms studied in class as a subroutine.
Problem 3: Prove, using induction, that the runtime of the linear time selection algorithm presented in
class is no slower than O(n2 ).
Problem 4: You have k identical bottles of water and a curious mind. You want to know, what is the
highest story of an n-story building that you can throw this bottle down in the grass without breaking it.
How you conduct your experiment depends on k. Design an algorithm for each k given below. For each
experiment, you must use the maximum possible number of bottles (in the worst case). Assume that n =
(a) k = 64
(b) k = 15
(c) k = 11
(c) k = 8
(d) k = 6