Just for practice (and not as a homework assignment) I have been trying to solve this problem (CLRS, 3rd edition, exercise 11.2-6):
Suppose we have stored
Repeat the following steps until an element is returned:
k
be the number of elements in the bucket.p
uniformly at random from 1 ... L
. If p <= k
then return the p
th element in the bucket.It should be clear that the procedure returns an element uniformly at random. We are sort of applying rejection sampling to the elements placed in a 2D array.
The expected number of elements per bucket is n / m
. The probability that the sampling attempt succeeds is (n / m) / L
. The expected number of attempts needed to find a bucket is therefore L * m / n
. Together with the O(L)
cost of retrieving the element from the bucket, the expected running time is O(L * (1 + m / n))
.