Interpolation search
Interpolation search is an algorithm for searching for a key in an array that has been ordered by numerical values assigned to the keys. It was first described by W. W. Peterson in 1957. Interpolation search resembles the method by which people search a telephone directory for a name : in each step the algorithm calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation. The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. This method will only work if calculations on the size of differences between key values are sensible.
By comparison, binary search always chooses the middle of the remaining search space, discarding one half or the other, depending on the comparison between the key found at the estimated position and the key sought — it does not require numerical values for the keys, just a total order on them. The remaining search space is reduced to the part before or after the estimated position. The linear search uses equality only as it compares elements one-by-one from the start, ignoring any sorting.
On average the interpolation search makes about log comparisons, where n is the number of elements to be searched. In the worst case it can make up to O notation|O] comparisons.
In interpolation-sequential search, interpolation is used to find an item near the one being searched for, then linear search is used to find the exact item.
Performance
Using big-O notation, the performance of the interpolation algorithm on a data set of size n is O; however under the assumption of a uniform distribution of the data on the linear scale used for interpolation, the performance can be shown to be O.Dynamic interpolation search extends the o bound to other distributions and also supports O insertion and deletion.
Practical performance of interpolation search depends on whether the reduced number of probes is outweighed by the more complicated calculations needed for each probe. It can be useful for locating a record in a large sorted file on disk, where each probe involves a disk seek and is much slower than the interpolation arithmetic.
Index structures like B-trees also reduce the number of disk accesses, and are more often used to index on-disk data in part because they can index many types of data and can be updated online. Still, interpolation search may be useful when one is forced to search certain sorted but unindexed on-disk datasets.
Adaptation to different datasets
When sort keys for a dataset are uniformly distributed numbers, linear interpolation is straightforward to implement and will find an index very near the sought value.On the other hand, for a phone book sorted by name, the straightforward approach to interpolation search does not apply. The same high-level principles can still apply, though: one can estimate a name's position in the phone book using the relative frequencies of letters in names and use that as a probe location.
Some interpolation search implementations may not work as expected when a run of equal key values exists. The simplest implementation of interpolation search won't necessarily select the first element of such a run.
Book-based searching
The conversion of names in a telephone book to some sort of number clearly will not provide numbers having a uniform distribution and further, it is well known that some names are much more common than others Similarly with dictionaries, where there are many more words starting with some letters than others. Some publishers go to the effort of preparing marginal annotations or even cutting into the side of the pages to show markers for each letter so that at a glance a segmented interpolation can be performed.Sample implementation
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.import
import std;
using std::vector;
/*
arr = arr
scenario 3. when low < high, arr < arr, key < arr or key > arr
scenario 4. when low < high, arr < arr, arr <= key <= arr
Now let's analyze scenario 4:
Once entering the "while" loop, low <= middle <= high
let's analyse after one loop, whether "low > high" will occur
After one loop:
case a1: "low" branch has been executed in this loop
arr < key <= arr
so we have middle < high
so after this loop, we have
low <= high
case a2: "high" branch has been executed in this loop
arr <= key < arr
so we have low < middle
so after this loop, we have
low <= high
so after one loop, we have "low <= high"
when we exit the "while" loop:
case b1: arr >= arr
In the last loop, if "low" branch is executed, we know
arr < k <= arr
arr >= arr
low <= high
so we have
arr < k <= arr = arr
In the last loop, if "high" branch is executed, we know
arr <= key < arr
arr >= arr
low <= high
so we have
arr = arr <= key < arr
case b2: && :
In the last loop, "low" must have been changed
so we have
arr < key
so we have
arr < key < arr
case b3: &&
In the last loop, "high" must have been changed
so we have
key < arr
so we have
arr < arr < key < arr
- /
template
static Rank interpolationSearch
/*
search "key" in the sorted array arr <= key
How to verify that the algorithm is correct?
Proof:
finiteness: after one loop, the width of decreases strictly
Fist, high <---- high - 1
scenario 1. when low = high
scenario 2. when low < high, key < arr or arr <= key
scenario 3. when low < high, arr <= key < arr
Now let's analyze scenario 3:
Once entering the "while" loop, low <= middle < high
when we exit the "while" loop:
case a1: key < arr
so "low" is changed in the last loop, we know
arr <= key < arr
case a2: arr <= key
so "high" is changed in the last loop, we know
key < arr, impossible
conclusion: we should return "low - 1"
- /
template
static Rank interpolationSearch
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 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 ; to beat that, the interpolation search, as written above, would be allowed no more than three iterations.