Interpolation Search
-
Interpolation Search can be considered as an improved version of Binary Search. Like Binary Search, Interpolation Search is used to find an element from a sorted list. In certain scenarios, Interpolation Search works better than Binary Search. This type of search works best, if the array contains numerical values only.
Difference between Interpolation Search and Binary Search is that each iteration of Binary Search always chooses the middle of the remaining search space and discards a half of the remaining items. But in Interpolation Search, instead of finding a middle node, a probe node is identified using Linear Interpolation mathematical method.
Below is the formula used to find the Probe Node. As you can see, this is most suited for array of numbers. If the array contains non-numeric values, modification is needed for the formula. Check the gif image below for better understanding.
probe = start + ( ( (key - arry[start]) * (end - start) ) / (arry[end] - arry[start]) )
The idea is that the probe node identified should be much closer to the searched item in most cases and so it is able to discard more items in one iteration. So the number of iterations required is less compared to Binary Search.
The code samples provided in this page and the Binary Search page uses the same array. But, you can see that the Interpolation Search finds the searched item in 1 or 2 iterations, whereas Binary Search requires 3 or 4 iterations for most numbers in the list.
Time complexity of Interpolation Search for average case O(log(log(n))) and for worst case is O(n). Space complexity is O(1).
Interpolation Search Uses
Interpolation search works better than Binary Search if the elements in the array are uniformly distributed. There are many practical use cases of Interpolation Search like predicting the price of a Share by going through the past prices. Interpolation techniques are used in Natural Language Processing and Image Processing algorithms.
-
How Interpolation Search Works
Interpolation Search is also a divide and conquer method. In Interpolation Search, instead of finding a middle node, a Probe Node is identified using Linear Interpolation mathematical method using the below formula.
probe = start + ( ( (key - arry[start]) * (end - start) ) / (arry[end] - arry[start]) )
Then like, Binary Search, this Probe Node is compared with the searched element and discards a part of the array till the result is obtained.
Check the below gif image to easily understand the search process.
-
Algorithm
Interpolation Search Algorithm flow is listed as below.
Input : A sorted list of elements.
Output : Element position, if found. Else not found message.- Start.
- Set start = 0, end = length of array, pos = -1.
- Check key between start and end, if true go to next step, else go to step 8.
- Find probe node.
- If probe node value = searched value, element found. Go to step 8.
- Else if probe node value < searched value, set start = probe node + 1. Go to step 4.
- Else, end = probe node - 1. Go to step 4.
- If pos > -1, element found. Display result.
- Else if pos = -1, element not found. Display not found message.
- Stop.
-
Code Samples
Below are few sample code implementations of Interpolation Search Algorithm in Python, Java, C# and JavaScript.