Sequential search of an ordered list

SEQUENTIAL SEARCH
  i := 1
  while (i<n and x>ai) do i := i+1
  if (x ≠ ai) then i := 0
  return i

CASE 1:  x is in the list

Assume x equally likely to be any of the a's.

A = ∑i =1 to n [ Prob(x =ai) * (number of comparisons to find x) ]

    = (1/n)∑i =1 to n-1( i+1 ) + (1/n)n = n/2 + 3/2 - 1/n

CASE 2:  x is not in the list

Assume x equally likely to be in each of the n+1 intervals.

A = ∑i =1 to n+1 [ Prob(ai-1 < x < ai) * (number of comparisons to find x) ]

    = (1/(n+1))∑i =1 to n-1( i+1 ) + (2/(n+1))n = n/2 + 2 - 3/(n+1)

CASE 3:  50% Case 1 & 50% Case 2

A = n/2 + 7/4 + O(1/n)

cf. Baase pp.36-37, simple sequential search of unordered list takes approximately 3n/4


Dan Hirschberg
Last modified: Oct 28, 2003