# Interpolation search algorithm

In this article, we are going to learn **interpolation search along with its algorithm, C++ program**.

Submitted by Radib Kar, on November 19, 2018

## Binary search vs Interpolation search

Though **binary search** is a great algorithm for searching with average time complexity only **logn**, **interpolation search** is found to be more efficient as per as time complexity is considered.

In **binary search**, it often chooses the middle of the remaining search space, discarding one half or the other, again depending on the comparison between the key value found at the middle position and the key value is sought. Where in **interpolation search**, we use the **concept of interpolation**. **Interpolation is a process of constructing new data point from an already given set of discrete points**.

### Interpolation

In computer science, one often has a number of data points which represent the values of a function for a limited number of values of the independent variable.

**Linear interpolation** takes two data points, **say(x _{1},y_{1}) & (x_{2},y_{2})**, and the

**interpolant**is given by:

**y=y**

_{1}+(y_{2}-y_{1})(x-x_{1})/(x_{2}-x_{1}) at point (x,y)## Interpolation search

This algorithm tries to follow kind of searching as we search in the dictionary. Like, we need to search a word **'include'** we start before the middle, but when we need to search **'test'**, we start searching from last. That means it's not necessary to partition the search space on basis of the only the middle element. This is because we know the words are ordered and our intuition says the binary search is a bad choice.

The **interpolation search** tries to improve the binary search, but the question is how to find the value. We know the bounds of the interval & we want to search closer to the searching element. This can be done using the formula: **K= (data-low)/(high-low)**

This **K** is a constant which helps us to narrow down the search space. For binary search **K** is **simply=(low+high)/2**

It's found that on an average interpolation search makes only **log(logn)** comparison to reach the searched element.

### When to use interpolation search?

Data set( array elements) is ordered & uniformly distributed.

### C++ implementation of interpolation search

#include <bits/stdc++.h> using namespace std; void interpolationSearch(int* a,int n,int data){ int low=0,k,high=n-1; while(low<=high){ k=low+(((data-a[low])*(high-low))/(a[high]-a[low])); if(data==a[k]){ cout<<"data found"<<endl; return; } if(data<a[k]) high=k-1; else low=k+1; } cout<<"data not found"<<endl; return; } int main(){ int n,data; // enter array length cout<<"enter no of elements\n"; cin>>n; int* a=(int*)(malloc(sizeof(int)*n)); //fill the array cout<<"enter elements................\n"; for(int i=0;i<n;i++) scanf("%d",&a[i]); cout<<"enter element to be searched"<<endl; cin>>data; interpolationSearch(a,n,data); return 0; }

**Output**

First run: enter no of elements 7 enter elements................ 5 10 15 19 24 31 37 enter element to be searched 10 data found --------------------------------- Second run: enter no of elements 6 enter elements................ 23 -11 45 78 -12 4 enter element to be searched 12 data not found

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Dynamic Programming (Components, Applications and Elements)
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!