7-5 Median-of-3 partition

One way to improve the RANDOMIZED-QUICKSORT\text{RANDOMIZED-QUICKSORT} procedure is to partition around a pivot that is chosen more carefully than by picking a random element from the subarray. One common approach is the median-of-3 method: choose the pivot as the median (middle element) of a set of 3 elements randomly selected from the subarray. (See exercise 7.4-6.) For this problem, let us assume that the elements of the input array A[1..n]A[1..n] are distinct and that n3n \ge 3. We denote the sorted output array by A[1..n]A'[1..n]. Using the median-of-3 method to choose the pivot element xx, define pi=Pr{x=A[i]}p_i = \Pr\{x = A'[i]\}.

a. Give an exact formula for pip_i as a function of nn and ii for i=2,3,,n1i = 2, 3, \ldots, n - 1. (Note that p1=pn=0p_1 = p_n = 0.)

b. By what amount have we increased the likelihood of choosing the pivot as x=A[(n+1)/2]x = A'[\lfloor (n + 1) / 2 \rfloor], the median of A[1..n]A[1..n], compared with the ordinary implementation? Assume that nn \to \infty, and give the limiting ratio of these probabilities.

c. If we define a "good" split to mean choosing the pivot as x=A[i]x = A'[i], where n/3i2n/3n / 3 \le i \le 2n / 3, by what amount have we increased the likelihood of getting a good split compared with the ordinary implementation? (Hint:\textit{Hint:} Approximate the sum by an integral.)

d. Argue that in the Ω(nlgn)\Omega(n\lg n) running time of quicksort, the median-of-3 method affects only the constant factor.

a. pip_i is the probability that a randomly selected subset of size three has the A[i]A'[i] as it's middle element. There are 6 possible orderings of the three elements selected. So, suppose that SS' is the set of three elements selected.

We will compute the probability that the second element of SS' is A[i]A'[i] among all possible 33-sets we can pick, since there are exactly six ordered 33-sets corresponding to each 33-set, these probabilities will be equal. We will compute the probability that S[2]=A[i]S'[2] = A[i]. For any such SS', we would need to select the first element from [i1][i - 1] and the third from i+1,,n{i + 1, \ldots , n}. So, there are (i1)(ni)(i - 1)(n - i) such 33-sets. The total number of 33-sets is (n3)=n(n1)(n2)6\binom{n}{3} = \frac{n(n - 1)(n - 2)}{6}. So,

pi=6(ni)(i1)n(n1)(n2).p_i = \frac{6(n - i)(i - 1)}{n(n - 1)(n - 2)}.

b. If we let i=n+12i = \lfloor \frac{n + 1}{2} \rfloor, the previous result gets us an increase of

6(n12)(nn+12)n(n1)(n2)1n\frac{6(\lfloor\frac{n - 1}{2}\rfloor)(n - \lfloor\frac{n + 1}{2}\rfloor)}{n(n - 1)(n - 2)} - \frac{1}{n}

in the limit nn going to infinity, we get

limn6(n12)(nn+12)n(n1)(n2)1n=32.\lim_{n \to \infty} \frac{\frac{6(\lfloor \frac{n - 1}{2} \rfloor)(n - \lfloor \frac{n + 1}{2} \rfloor)}{n(n - 1)(n - 2)}}{\frac{1}{n}} = \frac{3}{2}.

c. To save the messiness, suppose nn is a multiple of 33. We will approximate the sum as an integral, so,

i=n/32n/3n/32n/36(x2+nx+xn)n(n1)(n2)dx=6(7n3/81+3n3/18+3n2/18n2/3)n(n1)(n2), \begin{aligned} \sum_{i = n / 3}^{2n / 3} & \approx \int_{n / 3}^{2n / 3} \frac{6(-x^2 + nx + x - n)}{n(n - 1)(n - 2)}dx \\ & = \frac{6(-7n^3 / 81 + 3n^3 / 18 + 3n^2 / 18 - n^2 / 3)}{n(n - 1)(n - 2)}, \end{aligned}

which, in the limit nn goes to infinity, is 1327\frac{13}{27} which is a constant that >13>\frac{1}{3} as it was in the original randomized quicksort implementation.

d. Even though we always choose the middle element as the pivot (which is the best case), the height of the recursion tree will be Θ(lgn)\Theta(\lg n). Therefore, the running time is still Ω(nlgn)\Omega(n\lg n).