Algorithms Homework – Fall 2000
Using Figure 8.1 as a model, illustrate the operation of PARTITION on the array A =
13 19 9 5 12 8 7 4 11 2 6 21 i j j
6 19 9 5 12 8 7 4 11 2 13 21 i i j j
6 2 9 5 12 8 7 4 11 19 13 21 i ………………………… j
SPLIT = and
What value of q does PARTITION return when all elements in the array A[p…r] have the same value?
q = (p+r)/2, where p = index 0, and r = highest index
Give a brief argument that the running time of PARTITION on a subarray of size n is (n).
In the worst case, PARTITION must move the j pointer by one element (to the 2nd to last element), and the i pointer all the way to j, making a comparison at each element along the way. Since there are n comparisons made, the running time is (n) In the average (and best) case, PARTITION must move the j pointer to an element at or near the half-way point in the array and the i pointer all the way to j, making a comparison at each element along the way. Once again there are n comparisons made and the running time is (n)
Show that the running time of QUICKSORT is (n lg n) when all elements of array A have the same value.
T(n) = 2T(n/2) + (n)
CASE 2 of Master Theorem
f(n) lg n = (n lg n)
show that the running time of QUICKSORT is (n2) when the array A is sorted in nonincreasing order.
The recurrence in this case is
T(n) = T(n – 1) + (n)
= T(n – 2) + (n – 1) + (n)
= T(n – 3) + (n – 2) + (n – 1) + (n) … = (n) +
Banks often record transactions on an account in order of the times of the transactions, but many people like to receive their bank statements with checks listed in order by check number. People usually write checks in order by check number, and merchants usually cash them with reasonable dispatch. The problem of converting time-of-transaction ordering to check-number ordering is therefore the problem of sorting almost-sorted input. Argue that the procedure INSERTION-SORT would tend to beat the procedure QUICKSORT on this problem.
As the data being sorted becomes closer to sorted, insertion sort gets closer to (n) and quicksort gets closer to (n2). Therefore, there comes a point at which insertion sort performs better than quicksort. Further, while quicksort is still (n lg n) the constant multiple continues to grow as the data is more sorted. And with insertion sort, the constant multiple continues to shrink.
Suppose that the splits at every level of quicksort are in the proportion from 1 – to , where 0 ½ is a constant. Show that the minimum depth of a leaf in the recursion tree is approximately –lg n/lg and the maximum depth is approximately –lg n/lg (1 – ). (Don’t worry about the integer round-off.)
log1/n = (logbn)/(logb1/) = –(logbn)/(logb) = –(lgn)/(lg) log1/(1-)n = (logbn)/(logb1/(1-)) = –(logbn)/(logb(1-)) = –(lgn)/(lg(1-))
See Quiz 4 answer sheet.
During the running of the procedure RANDOMIZED-QUICKSORT, how many calls are made to the random-number generator RANDOM in the worst case? How does the answer change in the best case?
In both cases the answer is (n – 1)
Describe an implementation of the procedure RANDOM(a, b) that uses only fair coin flips. What is the expected running time of your procedure?
Do lg(b – a + 1) flips and store them as a bit vector where H = 1 and T = 0. 8.4-4
The running time of quicksort can be improved in practice by taking advantage of the fast running time of insertion sort when its input is “nearly” sorted. When quicksort is called on a subarray with fewer than k elements, let it simply...
Please join StudyMode to read the full document