Author: | Wojciech Muła |
---|---|
Added-on: | 2014-09-25 |
Contents
Interpolation search is the modification of binary search, where the index of a "middle" key is obtained from linear interpolation of values at start & end of a processed range:
a := start b := end key := searched key while a <= b loop t := (key - array[a])/(array[b] - array[a]) c := a + floor(t * (b - a)) -- in binary search just: c := (a + b)/2 if key = array[c] then return c else if key < array[c] then b := c - 1 else a := c + 1 endif end loop
The clear advantage over basic binary search is complexity O( log logn). When size of array is 1 million, then average number of comparison in binary search is