Consider the following algorithm for sorting a list:
Assume that shuffling and checking the list are both linear operations in terms of the number of elements in the list.
Let O(f(n)) be the average case performance of this algorithm, where n is the number of elements in the list. What is f(4)/f(1) ?
For example: if the average case performance is O( ), then , and .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The answer is 96 .
A list of size n has n ! possible permutations, so the chance of a random shuffle producing the correct order is 1 / n ! .
This is a geometric distribution with p = 1 / n ! . The mean value of the probability distribution of the number of Bernoulli trials needed for the first success is 1 / p . For this case, on average it will take n ! shuffles before the we reach a successful one.
Since shuffling and checking the list are both linear operations, the final average case performance is O ( n × n ! ) . f ( n ) = C ∗ ( n × n ! ) , for some coefficient C. f ( 4 ) / f ( 1 ) = 9 6 .