Interpolation search (sometimes referred to as extrapolation search) is an algorithm for searching for a given key value in an indexed array that has been ordered by the values of the key. It parallels how humans search through a telephone book for a particular name.
On average the interpolation search makes about log(log(n)) comparisons. In the worst case it can make up to O(n).
In each search step it 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.
CODE
public int interpolationSearch(int[] Array, int key){
int low = 0;
int high = Array.length – 1;
int mid;
while (Array [low] <= key && Array [high] >= key) {
mid = low + (high – low) / (Array[high] – Array[low]) *( key – Array[low]);
if (Array [mid] < key)
low = mid + 1;
else if (Array [mid] > key)
high = mid – 1;
else
return mid;
}
if (Array [low] == key)
return low;
else
return -1; // Not found
}