Assuming rand_between implement a sample from a uniform probability distribution in constant time, the expected runtime of this algorithm is Θ (log n). Unofficial sketch of evidence: the expected value of rand_between(l, r) is (l+r)/2 , the middle between them. Thus, each iteration is expected to miss half of the array (provided that the size is equal to the power of two), as well as one iteration of the binary search.
More formally, borrowing from the quickselect analysis, note that when you select a random midpoint, half the time it will be between ¼n and ¾n. Neither the left nor the right subarrays have more than ¾n elements. The other half of the time does not have more than n elements (obviously). This leads to a recurrence relation
T(n) = ½T(¾n) + ½T(n) + f(n)
where f (n) is the amount of work at each iteration. Subtracting ½T (n) on both sides, then doubling both sides, we have
½T(n) = ½T(¾n) + f(n) T(n) = T(¾n) + 2f(n)
Now, since 2f (n) = Θ (1) = Θ (n ᶜ log⁰ n), where c = log (1) = 0, by the theorem of the master it follows that T (n) = Θ (n⁰ log n) = Θ (log n).
Fred foo
source share