Consider the following algorithm for searching for a given number in an unsorted array having distinct values:
- Choose an at random from
- If , then Stop else Goto 1;
Assuming that is present in , what is the expected number of comparisons made by the algorithm before it terminates?
View worked solution
Answer: A
The algorithm describes a randomized search with replacement. Let's analyze the probability of finding the element in each step:
- Since the array has distinct elements and is guaranteed to be present, there is exactly one index such that .
- In each step, an index is chosen uniformly at random from the set of indices.
- The probability of success (finding ) in any single trial is:
- The probability of failure in any single trial is:
Since the choices in each step are independent, the number of trials required to find follows a Geometric distribution with success probability .
The expected number of trials (which is equal to the expected number of comparisons, as each trial makes exactly one comparison) is given by the expectation of a geometric random variable:
Thus, the expected number of comparisons made by the algorithm before it terminates is .
Correct Option: A
Free practice · Your answers on this page are not saved.
