# algorithm

Topics: Sorting algorithm, Quicksort, Merge sort Pages: 6 (1449 words) Published: December 21, 2013
﻿Algorithms Homework – Fall 2000

8.1-1Using 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

return 11,SPLIT = and

8.1-2What 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

8.1-3Give 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)

8.2-1Show 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)

8.2-2show 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) + 
= (n2)

8.2-3Banks 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.

8.2-4Suppose 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-))

8.2-5See Quiz 4 answer sheet.

8.3-2During 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)

8.3-3Describe 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-4The 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