DAA: Interpolation Search Algorithm

Interpolation Search Algorithm

There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces the search operation.

In accordance with binary search, the interpolation search is an enhanced algorithm. In binary search, as the array is sorted, it always goes to the middle element in each iteration. But interpolation works in a different manner; it may go to different indexes for the key being searched.

For example, if the element to be searched is in the start, the interpolation search will start to search elements from the beginning, not from the last or middle.

The position to start searching the element is calculated from the below formula:

position = low + [ (x - array[low])*(high - low) / (array[high] - arr[Low]) ]

Here,

x: The element to be searched

low: Starting index in our array

high: Ending index in our array

Now let us understand how the position is derived:

(Assumption) The elements in the array are linearly distributed.

We will generalize the equation of the straight line in coordinate geometry, i.e.,

                        y = m * x + c

Here y is the value in the array and x is the index of that value.

Now we insert the low, high and x in the equation -

 array[hi] = m * high + c ----(1)
 array[low] = m * low + c ----(2)
 x = m * position + c     ----(3)
 m = (array[high] - array[low] ) / (high - low)
 Subtracting equation (2) from (3)
 x - array[low] = m * (position - low)
 low + (x - array[low]) / m = position 

position = low + (x - array[low])  * (high - low) / (array[high] - array[low])

  1. In a loop, find the value of the position from the formula calculated above.
  2. If the element is found at that index, return and exit.
  3. If the key to be searched is less than array[position], using the above formula find the starting index from which element will be searched. Otherwise, find it in the right half array.
  4. Repeat the above step until the key is found.

C++ Code:

 #include <bits/stdc++.h>
 using namespace std;
 // If the x is found in the array return position of x otherwise, -1
 int interpolationSearch(int array[], int n, int x)
 {
     // The low value start from beginning of array and high points to (n-1)th element
     int low = 0, high = (n - 1);
     // Run a loop to find the element in the boundary
     while (low <= high && x >= array[low] && x <= array[high]) {
         if (low == high) {
             if (array[low] == x)
                 return low;
             return -1;
         }
         // Calculate the position from above formula
         int position = low + (((double)(high - low) / (array[high] - array[low])) * (x - array[low]));
         // If key to be searched is found return position
         if (array[position] == x)
             return position;
         // If key is larger, it is in upper part i.e, right subarray
         if (array[position] < x)
             low = position + 1;
         // If key is smaller, it is in the lower part i.e., left subarray
         else
             high = position - 1;
     }
     return -1;
 }
 int main() // Main function to call interpolation search
 {
     int array[] = { 10, 12, 13, 16, 18, 19, 20, 21 };
     int n = sizeof(array) / sizeof(array[0]);
     int x = 16; // Key to be searched
     int index = interpolationSearch(array, n, x);
     // If key was not found it will return -1
     if (index != -1)
         cout << "Key searched is found at index " << index;
     else
         cout << "key not found.";
     return 0;
 } 

C code:

 #include <stdio.h>
 // If the x is found in the array return position of x otherwise, -1
 int interpolationSearch(int array[], int n, int x)
 {
     // The low value start from beginning of array and high points to (n-1)th element
     int low = 0, high = (n - 1);
     // Run a loop to find the element in the boundary
     while (low <= high && x >= array[low] && x <= array[high]) {
         if (low == high) {
             if (array[low] == x)
                 return low;
             return -1;
         }
         // Calculate the position from above formula
         int position = low + (((double)(high - low) / (array[high] - array[low])) * (x - array[low]));
         // If key to be searched is found return position
         if (array[position] == x)
             return position;
         // If key is larger, it is in upper part i.e, right subarray
         if (array[position] < x)
             low = position + 1;
         // If key is smaller, it is in the lower part i.e, left subarray
         else
             high = position - 1;
     }
     return -1;
 }
 int main() // Main function to call interpolation search
 {
     int array[] = { 10, 12, 13, 16, 18, 19, 20, 21 };
     int n = sizeof(array) / sizeof(array[0]);
     int x = 16; // Key to be searched
     int index = interpolationSearch(array, n, x);
     // If key was not found it will return -1
     if (index != -1)
         printf("Key searched is found at index %d", index);
     else
         printf("key not found.");
     return 0;
 } 

Related Topics

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 minutes read.

DAA: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

4 minutes read.

Boyer Moore Algorithm

Boyer Moore Algorithm The Boyer Moore algorithm is a searching algorithm in which a string of length n and a pattern of length m is searched. It prints all the occurrences...

11 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 minutes read.

Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data,...

3 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 minutes read.