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: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 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.

Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have...

2 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: 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.

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: 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.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

DAA: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 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: 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.

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.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 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: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly 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 insertion sort technique works...

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: 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.

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: 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: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 minutes read.