EXAMPLE: To look up cat in a dictionary, you expect to find cat near the beginning.
INTERPOLATON SEARCH
i := 1
j := n
LO := ai
HI := aj
if x < LO then return 0
if x ≥ HI then i := j
loop invariant: x ≥ LO and x ≤ HI
while (i<j) do
m := [ i + (j-i)*(x-LO) / (HI-LO) ]
MID := am
if (x > MID) then
i := m+1
LO := MID
else if (x < MID) then
j := m-1
HI := MID
else
return MID
if (x ≠ ai) then i := 0
return i
The algorithm can be modified to use only one element comparison per loop iteration,
INTERPOLATON SEARCH
i := 1
j := n
LO := ai
HI := aj
if x < LO then return 0
if x ≥ HI then i := j
loop invariant: x ≥ LO and x ≤ HI
while (i<j) do
m := [ i + (j-i)*(x-LO) / (HI-LO) ]
prevent probe at last index, thereby avoiding infinte loop
if (m = j) then m := j-1
MID := am
if (x > MID) then
i := m+1
LO := MID
else
j := m
HI := MID
if (x ≠ ai) then i := 0
return i
EXAMPLE: 1,2,3,4,5,...,999,1000,109
Look for 1000