Interpolation search vs Binary search
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]){ // Calculate the position of the target element based on its value int pos = low + (((target - arr[low]) * (high - low)) / (arr[high] - arr[low])); // Check if the target element is at the calculated position if ( arr[pos] == target){ return pos; } // If the target element is less than the element at the calculated position, search the left half of the list if (arr[pos] > target){ high = pos - 1; } else { // If the target element is greater than the element at the calculated position, search the right half of the list 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); // cout << index << endl; if (index == -1){ cout << target << " not found in the list" << endl; } else { cout << target << " found at index " << index << endl; } } // The code is contributed by Gautam goel. |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main (String[] args) { System.out.println( "GFG!" ); } } |
Python3
def interpolation_search(arr, target): low = 0 high = len (arr) - 1 while low < = high and target > = arr[low] and target < = arr[high]: # Calculate the position of the target element based on its value pos = low + ((target - arr[low]) * (high - low)) / / (arr[high] - arr[low]) # Check if the target element is at the calculated position if arr[pos] = = target: return pos # If the target element is less than the element at the calculated position, search the left half of the list if arr[pos] > target: high = pos - 1 else : # If the target element is greater than the element at the calculated position, search the right half of the list 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() |
Javascript
function interpolation_search(arr, target){ let low = 0; let high = arr.length - 1; while (low <= high && target >= arr[low] && target <= arr[high]){ // Calculate the position of the target element based on its value let pos = low + Math.floor(((target - arr[low]) * (high - low)) / (arr[high] - arr[low])); // Check if the target element is at the calculated position if (arr[pos] == target){ return pos; } // If the target element is less than the element at the calculated position, search the left half of the list if (arr[pos] > target){ high = pos - 1; } else { // If the target element is greater than the element at the calculated position, search the right half of the list 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); } // The code is written by Nidhi goel. |
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]) { // Calculate the position of the target element based on its value int pos = low + (((target - arr[low]) * (high - low)) / (arr[high] - arr[low])); // Check if the target element is at the calculated position if (arr[pos] == target) { return pos; } // If the target element is less than the element at the calculated position, search the left half of the list if (arr[pos] > target) { high = pos - 1; } else { // If the target element is greater than the element at the calculated position, search the right half of the list 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); // Console.WriteLine(index); if (index == -1) { Console.WriteLine(target + " not found in the list" ); } else { Console.WriteLine(target + " found at index " + index); } } } |
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
Please Login to comment...