The following
C++ code example is a simple implementation. At each stage it computes a probe position then as with the binary search, moves either the upper or lower bound in to define a smaller interval containing the sought value. Unlike the binary search which guarantees a halving of the interval's size with each stage, a misled interpolation may reduce/i-case efficiency of O(
n). import ; import std; using std::vector; /* arr[low, high) is sorted, search the data "key" in this array, if "key" is found, return the corresponding index (NOT necessarily the highest possible index); if "key" is not found, just return low - 1 How to verify that the algorithm is correct? Proof: (finiteness: after one loop, the width of [low, high] decreases strictly ) Fist, high arr[high] scenario 4. when low high" will occur After one loop: case a1: "low" branch has been executed in this loop arr[middle] = arr[high] In the last loop, if "low" branch is executed, we know arr[low - 1] = arr[high] low = arr[high] low key): In the last loop, "low" must have been changed so we have arr[low - 1] arr[high]) In the last loop, "high" must have been changed so we have key static Rank interpolationSearch(vector& arr, const T& key, Rank low, Rank high) { high -= 1; int middle; int initialLow = low; while ((arr[low] static Rank interpolationSearch(vector& arr, const T& key, Rank low, Rank high) { high -= 1; assert(low Notice that having probed the list at index
mid, for reasons of loop control administration, this code sets either
high or
low to be not
mid but an adjacent index, which location is then probed during the next iteration. Since an adjacent entry's value will not be much different, the interpolation calculation is not much improved by this one step adjustment, at the cost of an additional reference to distant memory such as disk. Each iteration of the above code requires between five and six comparisons (the extra is due to the repetitions needed to distinguish the three states of and = via binary comparisons in the absence of a
three-way comparison) plus some messy arithmetic, while the
binary search algorithm can be written with one comparison per iteration and uses only trivial
integer arithmetic. It would thereby search an array of a million elements with no more than twenty comparisons (involving accesses to slow memory where the array elements are stored); to beat that, the interpolation search, as written above, would be allowed no more than three iterations. ==See also==