Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Implement Interpolation Search Algorithm in C++



The interpolation search is an improved version of the binary search. The list is divided using the mid element in the binary search whereas the interpolation search algorithm tries to locate the exact position using the interpolation formula. For this algorithm to work properly, the data collection should be in a sorted form and equally distributed.

In this article, we have a sorted array. Our task is to implement the interpolation search algorithm algorithm on this sorted array in C++. The formula used in this algorithm to find the position is given below:

Interpolation Position Search Formula

pos = start + [((key - array[start]) * (end - start)) / (array[end] - array[start])]

where,
start = starting index of the array, 
end = ending index of the array, 
key = element that you want to search, 
array[start] = first element of the array, and 
array[end] = last element of the array.

Example

Here is an example of interpolation search using the above formula:

array = {10, 20, 30, 40, 47, 50, 60, 70, 80, 100}
key = 47

start = 0, array[start] = 10
end = 9, array[end] = 100
pos = 3 (Using above formula)
key = array[3] = 40 < 47

start = (pos+1) = 4, array[start] = 47
end = 9, array[end] = 100
pos = 4 (Using above formula)
key = array[4] = 47 = 47

=> Key found at position 4

Steps to Implement Interpolation Search

The steps to implement interpolation search are as follows:

  • We have created an interpolationSearch() function that searches for a given key in a sorted array. It accepts four arguments: the array, the starting index, the ending index, and the key element to be searched.
  • We have used the while loop that runs as long as the key lies within the range of the array values from start to end.
  • Then, the if statement checks if all the elements between the start and end are the same. If the key matches, it returns the index otherwise, the loop breaks. It prevents the elements from getting divided by 0.
  • We have used the position formula that is mentioned above and stored it in the pos variable.
  • Then we used the if/else statement that checks where to find the key based on the value of the pos. If the element at the pos matches the key, the index is returned.
  • If the key is > pos, then we search in the right subarray using the index start = pos + 1. If the key is smaller, we use end = pos - 1; to search in the left subarray.
  • If the key is not found, -1 is returned, indicating that the element is not in the array.

C++ Program to Implement Interpolation Search

The following code uses the above steps to implement interpolation search in C++.

#include<iostream>
using namespace std;

int interpolationSearch(int array[], int start, int end, int key) {
    int dist, valRange, indexRange, pos;
    double fraction;
    
    while (start <= end && key >= array[start] && key <= array[end]) {
        if (array[start] == array[end]) {
            if (array[start] == key)
                return start;
            else
                break;
        }

        int pos = start + ((double)(key - array[start]) * (end - start)) / (array[end] - array[start]);

        if (array[pos] == key)
            return pos;
        if (array[pos] < key)
            start = pos + 1;
        else
            end = pos - 1;
    }
    return -1;
}

int main() {
    int arr[] = {10, 20, 30, 40, 47, 50, 60, 70, 80, 100};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 47;

    int loc = interpolationSearch(arr, 0, n - 1, key);
    cout << "Array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\nSearch Key: " << key << endl;
    if (loc >= 0)
        cout << "Item found at location: " << loc << endl;
    else
        cout << "Item is not found in the list." << endl;

    return 0;
}

The output of the above code is:

Array: 10 20 30 40 47 50 60 70 80 100 
Search Key: 47
Item found at location: 4

Time and Space Complexities

The time and space complexities of selection sort are as follows:

  • Time Complexity: The time complexity for the interpolation search is O(log log n) in the average case and O(n) in the worst case.

  • Space Complexity: The space complexity for the interpolation search is O(1).

Updated on: 2025-04-21T14:40:31+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements