Given a sorted integer array and a target, determine if the target exists in the array or not using an interpolation search algorithm. If the target exists in the array, return the index of it.

For example,

Input:
 
arr[] = [2, 3, 5, 7, 9]
target = 7
 
Output: Element found at index 3
 
 
Input:
 
arr[] = [1, 4, 5, 8, 9]
target = 2
 
Output: Element not found

Practice this problem

Interpolation search is an algorithm similar to binary search for searching for a given target value in a sorted array. It parallels how humans search through a telephone book for a particular name, the target value by which the book’s entries are ordered.

 
We know that binary search always chooses the middle of the remaining search space, discarding one half or the other depending on the comparison result between the mid-value and the target value. The remaining search space is reduced to the part before or after the mid-position.

By comparison, at each search step, Interpolation search calculates wherein the remaining search space the target might be present, based on the low and high values of the search space and the target’s value. The value found at this estimated position is then compared to the target value. If it’s not equal, then the remaining search space is reduced to the part before or after the estimated position depending on the comparison. This method will only work if calculations on the size of differences between target values are sensible.

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. It computes a mid-position at each iteration and then, as with the binary search, moves either the upper or lower bound in to define a smaller interval containing the target value. Unlike the binary search, which guarantees half search space size with each iteration, a poor interpolation may reduce/increase the mid-index by only one, resulting in a worst-case efficiency of O(n) for an input containing n items.

C


Download  Run Code

Output:

Element found at index 1

Java


Download  Run Code

Output:

Element found at index 1

Python


Download  Run Code

Output:

Element found at index 1

Performance

Each iteration of the above code requires between five and six comparisons. On average, the interpolation search makes about log(log(n)) comparisons if the elements are uniformly distributed, where n is the total number of elements to be searched. In the worst case, it can make up to O(n) comparisons. The worst-case might happen when the numerical values of the targets increase exponentially.

Note: This article doesn’t provide an in-depth analysis of how interpolation search works but aims to provide just an overview of achieving better performance than a binary search algorithm for a sorted array.

 
References: https://en.wikipedia.org/wiki/Interpolation_search