8.1-1 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
return 11, SPLIT = and
8.1-2 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
8.1-3 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)
8.2-1 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)
8.2-2 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) + = (n2)
8.2-3 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