Interpolation search vs Binary search
Last Updated :
11 Oct, 2023
Interpolation search works better than Binary Search for a Sorted and Uniformly Distributed array.
Binary Search goes to the middle element to check irrespective of search-key. On the other hand, Interpolation Search may go to different locations according to search-key. If the value of the search-key is close to the last element, Interpolation Search is likely to start search toward the end side.
Interpolation search and binary search are both algorithms for searching for a specific element in a sorted list or array. Both algorithms have an average-case time complexity of O(log n), which means that the time required to perform the search grows logarithmically with the size of the list.
However, there are some key differences between interpolation search and binary search:
Interpolation search estimates the position of the target element based on the values of the elements surrounding it, while binary search always starts by checking the middle element of the list.
Interpolation search is more efficient than binary search when the elements in the list are uniformly distributed, while binary search is more efficient when the elements in the list are not uniformly distributed.
Interpolation search can take longer to implement than binary search, as it requires the use of additional calculations to estimate the position of the target element.
Example :
C++
#include<bits/stdc++.h>
using namespace std;
int interpolation_search( int arr[], int target, int n){
int low = 0;
int high = n - 1;
while (low <= high && target >= arr[low] && target <= arr[high]){
int pos = low + (((target - arr[low]) * (high - low)) / (arr[high] - arr[low]));
if ( arr[pos] == target){
return pos;
}
if (arr[pos] > target){
high = pos - 1;
}
else {
low = pos + 1;
}
}
return -1;
}
int main(){
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof (arr)/ sizeof ( int );
int target = 5;
int index = interpolation_search(arr, target, n);
if (index == -1){
cout << target << " not found in the list" << endl;
}
else {
cout << target << " found at index " << index << endl;
}
}
|
Java
class GFG {
public static int interpolationSearch( int [] arr, int target) {
int low = 0 ;
int high = arr.length - 1 ;
while (low <= high && target >= arr[low] && target <= arr[high]) {
int pos = low + (((target - arr[low]) * (high - low)) / (arr[high] - arr[low]));
if (arr[pos] == target) {
return pos;
}
if (arr[pos] > target) {
high = pos - 1 ;
} else {
low = pos + 1 ;
}
}
return - 1 ;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int target = 5 ;
int index = interpolationSearch(arr, target);
if (index == - 1 ) {
System.out.println(target + " not found in the list" );
} else {
System.out.println(target + " found at index " + index);
}
}
}
|
Python3
def interpolation_search(arr, target):
low = 0
high = len (arr) - 1
while low < = high and target > = arr[low] and target < = arr[high]:
pos = low + ((target - arr[low]) * (high - low)) / / (arr[high] - arr[low])
if arr[pos] = = target:
return pos
if arr[pos] > target:
high = pos - 1
else :
low = pos + 1
return - 1
def main():
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
target = 5
index = interpolation_search(arr, target)
if index = = - 1 :
print (f "{target} not found in the list" )
else :
print (f "{target} found at index {index}" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static int InterpolationSearch( int [] arr, int target, int n)
{
int low = 0;
int high = n - 1;
while (low <= high && target >= arr[low] && target <= arr[high])
{
int pos = low + (((target - arr[low]) * (high - low)) / (arr[high] - arr[low]));
if (arr[pos] == target)
{
return pos;
}
if (arr[pos] > target)
{
high = pos - 1;
}
else
{
low = pos + 1;
}
}
return -1;
}
static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int n = arr.Length;
int target = 5;
int index = InterpolationSearch(arr, target, n);
if (index == -1)
{
Console.WriteLine(target + " not found in the list" );
}
else
{
Console.WriteLine(target + " found at index " + index);
}
}
}
|
Javascript
function interpolation_search(arr, target){
let low = 0;
let high = arr.length - 1;
while (low <= high && target >= arr[low] && target <= arr[high]){
let pos = low + Math.floor(((target - arr[low]) * (high - low)) / (arr[high] - arr[low]));
if (arr[pos] == target){
return pos;
}
if (arr[pos] > target){
high = pos - 1;
}
else {
low = pos + 1;
}
}
return -1;
}
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let target = 5;
let index = interpolation_search(arr, target);
if (index == -1){
console.log(target, " not found in the list" );
}
else {
console.log(target, " found at index " , index);
}
|
Output
5 found at index 4
On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.
Time Complexity : O(log(log n))
space complexity : O(1)
Interpolation Search Article
Sources:
http://en.wikipedia.org/wiki/Interpolation_search