In the previous part we determined the average-case running time of linear search when **x**, the element we are looking for, appears at least one time in a given array **A**. In this part, I will show a simpler way to determine the average-case running time.

Instead of thinking about the probability that the first copy of **x** appears at index **i**, let’s determine the probability that **A[i]** will be examined. Let’s denote this event by **E _{i}**. Also let’s denote the event that

**x**has been found by

**E**. Note that the running time is just the number of elements that we have examined.

_{m}Let **I** be the set of indices such that for all **i** in **I**: **A[i] != x**.

Define the random variable **X _{i}** over I as follows:

X

_{i}= 1 if

**E**occurs, 0 otherwise.

_{i}Define the random variable **M** as follows:

M = 1 if **E _{m}** occurs, and 0 otherwise.

Note that only one copy of **x** from **A** is examined and all other copies must appear after it and so they will never be examined.

And finally, let **X** by the running time of linear search, then:

X = M + X_{1} + X_{2} + … + X_{n-k}.

Therefore,

E[X] = E[M] + E[X_{1}] + E[X_{2}] + … + E[X_{n-k}].

We know that **E[M] = Pr{M=1} = 1** since **k>=1**. Now we need to compute **E[X _{i}]** or

**Pr{X**over

_{i}=1}**I**. It is not hard to see that an element that is not

**x**is compared only if it appeared before any of the

**k**copies of

**x**. It’s like asking what’s the probability of choosing that particular element out of the

**k+1**elements containing

**k**

**x**‘s and one none

**x**? The answer is obvious:

**1/(k+1)**. Now back to the expected running time:

E[X] = 1 + (n-k)/(k+1) = (k+1+n-k)/(k+1) = (n+1)/(k+1). Which is of course the same result we got in Part 2.

Recall that we still have to prove the lemma from Part 2. This turns out be tricky and the next part will be devoted for the proof.