Skip to content

7.2 Performance of quicksort

7.2-1

Use the substitution method to prove that the recurrence T(n)=T(n1)+Θ(n)T(n) = T(n - 1) + \Theta(n) has the solution T(n)=Θ(n2)T(n) = \Theta(n^2), as claimed at the beginning of section 7.2.

We represent Θ(n)\Theta(n) as c2nc_2n and we guess that T(n)c1n2T(n) \le c_1n^2,

T(n)=T(n1)+c2nc1(n1)2+c2n=c1n22c1n+c1+c2n(2c1>c2,nc1/(2c1c2))c1n2. \begin{aligned} T(n) & = T(n - 1) + c_2n \\ & \le c_1(n - 1)^2 + c_2n \\ & = c_1n^2 - 2c_1n + c_1 + c_2n & (2c_1 > c_2, n \ge c_1 / (2c_1 - c_2)) \\ & \le c_1n^2. \end{aligned}

7.2-2

What is the running time of QUICKSORT\text{QUICKSORT} when all elements of the array AA have the same value?

It is Θ(n2)\Theta(n^2), since one of the partitions is always empty (see exercise 7.1-2.)

7.2-3

Show that the running time of QUICKSORT\text{QUICKSORT} is Θ(n2)\Theta(n^2) when the array AA contains distinct elements and is sorted in decreasing order.

If the array is already sorted in decreasing order, then, the pivot element is less than all the other elements. The partition step takes Θ(n)\Theta(n) time, and then leaves you with a subproblem of size n1n − 1 and a subproblem of size 00. This gives us the recurrence considered in 7.2-1. Which we showed has a solution that is Θ(n2)\Theta(n^2).

7.2-4

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 numbers. People usually write checks in order by check number, and merchants usually cash the 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\text{INSERTION-SORT} would tend to beat the procedure QUICKSORT\text{QUICKSORT} on this problem.

The more sorted the array is, the less work insertion sort will do. Namely, INSERTION-SORT\text{INSERTION-SORT} is Θ(n+d)\Theta(n + d), where dd is the number of inversions in the array. In the example above the number of inversions tends to be small so insertion sort will be close to linear.

On the other hand, if PARTITION\text{PARTITION} does pick a pivot that does not participate in an inversion, it will produce an empty partition. Since there is a small number of inversions, QUICKSORT\text{QUICKSORT} is very likely to produce empty partitions.

7.2-5

Suppose that the splits at every level of quicksort are in proportion 1α1 - \alpha to α\alpha, where 0<α1/20 < \alpha \le 1 / 2 is a constant. Show that the minimum depth of a leaf in the recursion tree is approximately lgn/lgα-\lg n / \lg\alpha and the maximum depth is approximately lgn/lg(1α)-\lg n / \lg(1 - \alpha). (Don't worry about integer round-off.)

The minimum depth corresponds to repeatedly taking the smaller subproblem, that is, the branch whose size is proportional to α\alpha. Then, this will fall to 11 in kk steps where 1αkn1 \approx \alpha^kn. Therefore, klogα1/n=lgnlgαk \approx \log_\alpha 1 / n = -\frac{\lg n}{\lg\alpha}. The longest depth corresponds to always taking the larger subproblem. we then have an identical expression, replacing α\alpha with 1α1 − \alpha.

7.2-6 \star

Argue that for any constant 0<α1/20 < \alpha \le 1 / 2, the probability is approximately 12α1 - 2\alpha that on a random input array, PARTITION\text{PARTITION} produces a split more balanced than 1α1 - \alpha to α\alpha.

In order to produce a worse split than 1α1 - \alpha to α\alpha, PARTITION\text{PARTITION} must pick a pivot that will be either within the smallest αn\alpha n elements or the largest αn\alpha n elements. The probability of either is (approximately) αn/n=α\alpha n / n = \alpha and the probability of both is 2α2\alpha. Thus, the probability of having a better partition is the complement, 12α1 - 2\alpha.