Performance - recurrence for the expected number of recursive calls

199 Views Asked by At

Here's what the following code is doing:

The randomized algorithm selects the k-th smallest element in an unsorted array A[1...n], 1 < k < n.

For simplicity of analysis it is assumed that all the elements are diefferent, so that the k-th smallest is well-defined: the element with exactly k-1 elements smaller than it.

RandomSelect(A[1...n], k)
  p <- Random(1, n)
  r <- Partition(A[1...n], p)
  if k < r then
     return (RandomSelect(A[1...r - 1], k))
  else if k > r then
     return (RandomSelect(A[r + 1...n], k - r))
  else
    return A[r]

I know that Partition runs in linear time.

Partition splits the array into two parts corresponding to the elements smaller and larger than the pivot, placed to the left and right of the pivot, whose position is returned by the function.

Random (1, n) returns an integer in 1, 2,..., n uniformly at random (each integer equally likely).

I do not really understand how to write a recurrence for the expected number of recursive calls the algorithm performs. Hopefully someone can help me, please

I think I'm supposed to write a recursive routine that will return the expected number of calls for an input array of size n. And the algorithm selects the k-th smallest element of this array A[1..n] (and A[1..n] is unsorted)