RandomSelect(A, i, j, r) p = random(i, j) // random integer with i <= p <= j k = Partition(A, i, j, p) // partition array s = k - i + 1 // position of pivot in interval if r < s return RandomSelect(A, i, k-1, r) else if r > s return RandomSelect(A, k+1, j, r - s) else return A[k]The subroutine Partition(A, i, j, p) compares A[p] with each element in A[i..j], and rearranges the elements into three parts. It returns the new index of the pivot element A[p].
What is the exact probability that RandomSelect compares the ith smallest and jth smallest element in the input array? (The correct answer is a simple function of i, j, and r.)
What is the expected running time of RandomSelect, as a function of n and r? Can you give the exact number of comparisons?
What is the expected number of times that RandomSelect calls itself recursively?