We will break down this question into subparts as follows.
First, let us call the variant of Quicksort in
this exercise the , for any k ≥ 2.
In this variant, we stop the recurrence
when the size of the subarray to be
sorted is < k. The standard quicksort is the case k=2.
Insertion sort is a very simple algorithm described in chapter 2 (p.15).
(i) Let the array A contain the output of the
k-Truncated Quicksort (A is also going to be the input
for our insertion sort). Assume the input numbers are distinct.
What can you say about the ordering in A, without knowing
anything about how Quicksort makes its random choices?
(ii) Argue that the expected time of the k-Truncated Quicksort
algorithm is O(n log(n/k)).
(iii) Argue that the worst case time of insertion sort on
the output of the k-Truncated Quicksort algorithm is O(kn).
(iv) Suppose the time for (ii) is C1 nlog(n/k)
and the time for (iii) is C2 kn.
Describe how you would chose k to minimize the sum
|
Find the second largest in at most n+\ceillgn − 2 comparisons. HINT: set up a binary tree T with n leaves and height \ceillgn. Place the numbers in leaves of T and use it as a ``tournament'' to compute the largest. Now, ask yourself what information do you already possess about the next largest. Then, find it with the help of T.
Please help Professor Olay with his oil problem.
Suppose that we have a version of the median algorithm whose
recurrence is
|
(i) Show that if c1+c2 < 1, we will have the solution T(n)=Θ(n). NOTE: you must show two facts: T(n)=O(n) and T(n)=Ω(n). But one of them is ``trivial'' (but you still need to note it in your answer).
(ii) Suppose c1+c2=1. Guess what T(n) should be, and try to prove the best upper and lower bound you can.
HINT: For both (i) and (ii), use an induction argument. To guess the correct value of T(n), think of the Master theorem. For instance, suppose you guessed that T(n) ≤ C n3 for n ≥ n0 large enough. Attempt an induction proof, keeping C, n0 undetermined (the proof will suggest what C, n0 should be).
Implementing the division method for a radix-128 number.
This exercise is to help you understand perfect hashing. Let U=\ZZp for some prime p, and we are given a set K\ib U of n keys to be stored. We use a 2-level table structure: a primary table of size n and secondary tables whose total size is at most 4n. Each hash function has the form h(x) = (((ax+b) p) m) where m is the table size, and a, b are the parameters determining h. Here the prime number p and n are fixed throughout. Let us go into implementation details.
Suppose the primary hash function [^h]: K→ \ZZn
is fixed (in , it means you have
chosen the constants a and b).
For each i ∈ \ZZn, let
Bi = [^h]−1(i) = {k ∈ K: [^h](k)=i} be the
ith bucket. Let ni = |Bi| and we choose mi = ni2.
Suppose also that the secondary hash functions,
|
|
(i) How would you implement this hashing scheme? There are two parts in answering this question: describe your data structure, and describe the ``Hash Search'' algorithm which uses this data structure. (In class, we call the Hash Search algorithm the ``LookUp'' algorithm in class.) ASSUMPTION: each k ∈ U can be stored in one entry of your array. PLEASE make explicit any other reasonable assumptions you need.
HINTS: Where would you store the constants ai, bi, mi, and how would you organize the secondary tables. We suggest using two arrays: array T for the primary table, and an array S for all the secondary tables.
(ii) Suppose your keys are ASCII strings of arbitrary length. So we no longer assume that they fit into a single array entry. Continue to assume that n=|K| can fit into an array entry. How would you modify the above scheme?
(iii)
In part (i) we assume the hash functions were given to you.
But how can we find a suitable [^h]?
Here is the method: pick randomly a,b ∈ \ZZp where a ≠ 0
as the parameters for the function [^h]. Then check if
|
Question A: how may times do you expect to do this test until it is passed? Question B: Give more details about how you would implement this test, and say how much time is needed for each test.