In this problem, we use indicator random variables to analyze the RANDOMIZED-SELECT procedure in a manner akin to our analysis of RANDOMIZED-QUICKSORT in Section 7.4.2.
As in the quicksort analysis, we assume that all elements are distinct, and we rename the elements of the input array A as z1โ,z2โ,โฆ,znโ, where ziโ is the ith smallest element. Thus, the call RANDOMIZED-SELECT(A,1,n,k) returns zkโ.
For 1โคi<jโคn, let
Xijkโ=I {ziโ is compared with zjโ sometime during the execution of the algorithm to find zkโ}.
a. Give an exact expression for E[Xijkโ]. (Hint: Your expression may have different values, depending on the values of i, j, and k.)
b. Let Xkโ denote the total number of comparisons between elements of array A when finding zkโ. Show that
E[Xkโ]โค2(i=1โkโj=kโnโjโi+11โ+j=k+1โnโjโk+1jโkโ1โ+i=1โkโ2โkโi+1kโiโ1โ).
c. Show that E[Xkโ]โค4n.
d. Conclude that, assuming all elements of array A are distinct, RANDOMIZED-SELECT runs in expected time O(n).