given the following algorithm, provide an exact calculation for the expected number of total comparisons made by the algorithm when the given array contains a random permutation of the numbers 0,1,...,n-1 (they are all distinct)

As the program loops through the array, when the index = i, mx is the largest so far, and the comparison will succeed when the next one, a[i] is larger than any of the previous entries.

For an array of size n, the probability that the largest entry is is the last position is 1/n, since any of the positions are equally likely. So the probability that the largest entry is NOT last is (n-1)/n. In the last time through the loop. Thus there will be 1 + (n-1)/n comparisons for that iteration, plus all of the comparisons from previous iterations.

Now consider an array of size 1. The comparisons are never made.

For an array of size 2, the number of comparisons made is 1 (the comparison with mx), which will fail (n-1)/n, or 1/2, of the time. Thus the average number of comparisons for this step will be (1 + 1/2), for a total of(1 + 1/2).

For an array of size three, the number of comparisons made is 1 (again, the comparison with mx), and (n-1)/n = 2/3. Thus for the last comparison only, the number of expected comparisons is (1 + 2/3). But to reach this point there has already been (1 + 1/2) comparisons from the previous time through the loop. So for n = 3, the number of comparisons is (1 + 1/2) + (1 + 2/3).

For an array of size n, the probability that the largest entry is is the last position is 1/n, since any of the positions are equally likely. So the probability that the largest entry is NOT last is (n-1)/n. In the last time through the loop. Thus there will be 1 + (n-1)/n comparisons for that iteration, plus all of the comparisons from previous iterations.

Just so I can attempt to understand better. We are saying the probability that the largest entry is not last is 1 - [the probability that the largest entry is last] = (n-1)/n.

Now the total number of comparisons = [1 comparison when the last entry is maximum] OR [(n-1)/n comparisons when the last entry is not maximum]
= 1 + (n-1)/n

Just so I can attempt to understand better. We are saying the probability that the largest entry is not last is 1 - [the probability that the largest entry is last] = (n-1)/n.

Now the total number of comparisons = [1 comparison when the last entry is maximum] OR [(n-1)/n comparisons when the last entry is not maximum]
= 1 + (n-1)/n

Is this right?

As to the first statement, yes. The probability of the largest element is last is , so the probability it is not last is , or .

The second I am not sure about. The total number of comparisons is 1 when the last entry is maximum, and 2 when the last entry is not maximum. Since the probability that the last element is maximum is and the probability that it is not maximum is , the way I would say it is the total number of comparisons is , which simplifies to .

The second I am not sure about. The total number of comparisons is 1 when the last entry is maximum, and 2 when the last entry is not maximum. Since the probability that the last element is maximum is and the probability that it is not maximum is , the way I would say it is the total number of comparisons is , which simplifies to .