Processing math: 100%
Close
Close Window

Chapter 27 Searching

Show Source |    | About   «  27.1. Chapter Introduction: Search   ::   Contents   ::   27.3. Search in Sorted Arrays  »

27.2. Analyzing Search in Unsorted Arrays

You already know the simplest form of search: the sequential search algorithm. Sequential search on an unsorted list requires Θ(n) time in the worst case.

How many comparisons does linear search do on average? A major consideration is whether K is in list L at all. We can simplify our analysis by ignoring everything about the input except the position of K if it is found in L. Thus, we have n+1 distinct possible events: That K is in one of positions 0 to n1 in L (each position having its own probability), or that it is not in L at all. We can express the probability that K is not in L as

P(KL)=1ni=1P(K=L[i])

where P(x) is the probability of event x.

Let pi be the probability that K is in position i of L (indexed from 0 to n1. For any position i in the list, we must look at i+1 records to reach it. So we say that the cost when K is in position i is i+1. When K is not in L, sequential search will require n comparisons. Let pn be the probability that K is not in L. Then the average cost T(n) will be

T(n)=npn+n1i=0(i+1)pi.

What happens to the equation if we assume all the pi 's are equal (except pn)?

T(n)=pnn+n1i=0(i+1)p=pnn+pni=1i=pnn+pn(n+1)2=pnn+1pnnn(n+1)2=n+1+pn(n1)2

Depending on the value of pn, n+12T(n)n.

   «  27.1. Chapter Introduction: Search   ::   Contents   ::   27.3. Search in Sorted Arrays  »

nsf
Close Window