I came to know that Interpolation Search is a modification of Binary Search where in binary search the input is divided into two equal halves in each iteration by computing
mid = (low + high) / 2
and in Interpolation search the mid is computed as
mid = low + (key - arr[low]) * ((high - low) / (arr[high] - arr[low]))
Now I need to understand this formula of calculating mid
in interpolation search.
Ref: https://en.wikipedia.org/wiki/Interpolation_search#Sample_implementation
Interpolation search uses the following formula to calculate the mid-position where A[low…high] is our search space, and target is the given target: mid = low + ((target – A[low]) * (high – low) / (A[high] – A[low])); Following is the C, Java, and Python implementation of interpolation search.
The interpolation search algorithm improves the binary search algorithm. The formula for finding a value is: K = data-low/high-low. K is a constant which is used to narrow the search space. In the case of binary search, the value for this constant is: K=(low+high)/2.
The best case for Interpolation Search happens when the middle (our approximation) is the desired key. This makes the best case time complexity is O(1). In the worst-case scenario, we will need to traverse all of the elements in the array, resulting in the O(n) time complexity.
You can think of array arr
as a function f
that acts on index and return a value, which is monotone (because array is sorted). So you have your initial data f(low) = m
and f(high) = M
. Now you can interpolate your function f
with a straight line, which is quite reasonable to do because your f
is monotone and you have only 2 points.
So your interpolation should be line (linear function) that pass throw points (low, m)
and (high, M)
. This is it's equation
(y - f(low))/(f(high) - f(low)) = (x - low)/(high - low)
So y
here is the element of search space and x
is from domain (x is index of array). So if your function f
would be the same as it's interpolation, then index of your key
would be:
x = low + (high - low)*(key - f(low))/(f(high) - f(low))
So, assuming that your function f
is close to it's interpolation, you should just check the value f
at x
to see if it is the goal. Otherwise you just shrink your interpolation interval.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With