Interpolation Search - GeeksforGeeks

Interpolation Search

Last Updated : 31 Oct, 2026

Given a sorted array of n uniformly distributed values arr[], write a function to search for a particular element x in the array. 

  • Linear Search finds the element in O(n) time, Jump Search takes O(√n) time and Binary Search takes O(log n) time. 
  • The Interpolation Search is an improvement over Binary Search when the values are uniformly distributed.

Binary Search always goes to the middle element to check. On the other hand, interpolation search may go to different locations according to the value of the key being searched. For example, if the value of the key is closer to the last element, interpolation search is likely to start search toward the end side.

To find the position to be searched, it uses the following formula. 

There are many different interpolation methods. Let us consider simple yet popular linear interpolation. The linear interpolation formula estimates the position of the target element based on its value relative to the elements at the current search boundaries. Search uses this idea to estimate the position similar to word search in a dictionary.

arr[] → The sorted array in which the element is to be searched.
x → The target element to be searched.
lo → The starting index of the search range.
hi → The ending index of the search range.

pos = lo + ((x - arr[lo]) * (hi - lo)) / (arr[hi] - arr[lo])

Derivation of the Position Formula

Linear interpolation estimates a value between two data points, (x1, y1) and (x2, y2), assuming a straight-line relationship. Interpolation

Assume that the array elements are linearly distributed. The general equation of a straight line is:

y = m * x + c

For the boundary elements and target x:

arr[hi] = m * hi + c ...(1)
arr[lo] = m * lo + c ...(2)
x = m * pos + c ...(3)

Subtracting equation (2) from equation (3):

x - arr[lo] = m * (pos - lo)

Therefore: pos = lo + (x - arr[lo]) / m

Since: m = (arr[hi] - arr[lo]) / (hi - lo)

Substituting the value of m, we get the final formula:

pos = lo + ((x - arr[lo]) * (hi - lo)) / (arr[hi] - arr[lo])

Illustration :


Recursive Approach

  • Initialize lo = 0 and hi = n - 1.
  • Check whether x lies within arr[lo] and arr[hi]. If not, return -1.
  • Calculate the probable position pos using the interpolation formula.
  • If arr[pos] == x, return pos.
  • If arr[pos] < x, recursively search the right subarray; otherwise, search the left subarray.
  • Repeat until the element is found or the search range becomes invalid.
C++
#include <iostream>
#include <vector>
using namespace std;

// Recursive function to perform interpolation search
int interpolationSearch(vector<int>& arr, int lo, int hi, int x) {
    if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        
        // Handle equal boundary elements
        if (arr[lo] == arr[hi])
            return (arr[lo] == x) ? lo : -1;

        // Estimate the target position
        int pos = lo + (int)(((long long)(x - arr[lo]) * (hi - lo)) /
                             (arr[hi] - arr[lo]));

        if (arr[pos] == x)
            return pos;

        // Search the appropriate half recursively
        if (arr[pos] < x)
            return interpolationSearch(arr, pos + 1, hi, x);

        return interpolationSearch(arr, lo, pos - 1, x);
    }

    return -1;
}

int main() {
    vector<int> arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                       23, 24, 33, 35, 42, 47};
    int x = 18;

    int index = interpolationSearch(arr, 0, arr.size() - 1, x);

    if (index != -1)
        cout << "Element found at index " << index;
    else
        cout << "Element not found.";

    return 0;
}
C
#include <stdio.h>

// Recursive function to perform interpolation search
int interpolationSearch(int arr[], int lo, int hi, int x) {
    if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        // Handle equal boundary elements
        if (arr[lo] == arr[hi])
            return (arr[lo] == x) ? lo : -1;

        // Estimate the target position
        int pos = lo + (int)(((long long)(x - arr[lo]) * (hi - lo)) /
                             (arr[hi] - arr[lo]));

        if (arr[pos] == x)
            return pos;

        // Search the appropriate half recursively
        if (arr[pos] < x)
            return interpolationSearch(arr, pos + 1, hi, x);

        return interpolationSearch(arr, lo, pos - 1, x);
    }

    return -1;
}

int main() {
    int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                 23, 24, 33, 35, 42, 47};
    int x = 18;
    int n = sizeof(arr) / sizeof(arr[0]);

    int index = interpolationSearch(arr, 0, n - 1, x);

    if (index != -1)
        printf("Element found at index %d\n", index);
    else
        printf("Element not found.\n");

    return 0;
}
Java
import java.util.Arrays;

class GFG {

    // Recursive function to perform interpolation search
    static int interpolationSearch(int[] arr, int lo, int hi, int x) {
        if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
            // Handle equal boundary elements
            if (arr[lo] == arr[hi])
                return (arr[lo] == x) ? lo : -1;

            // Estimate the target position
            int pos = lo + (int)(((long)(x - arr[lo]) * (hi - lo)) /
                                 (arr[hi] - arr[lo]));

            if (arr[pos] == x)
                return pos;

            // Search the appropriate half recursively
            if (arr[pos] < x)
                return interpolationSearch(arr, pos + 1, hi, x);

            return interpolationSearch(arr, lo, pos - 1, x);
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                     23, 24, 33, 35, 42, 47};
        int x = 18;

        int index = interpolationSearch(arr, 0, arr.length - 1, x);

        if (index != -1)
            System.out.println("Element found at index " + index);
        else
            System.out.println("Element not found.");
    }
}
Python
# Recursive function to perform interpolation search
def interpolationSearch(arr, lo, hi, x):
    if lo <= hi and arr[lo] <= x <= arr[hi]:
        # Handle equal boundary elements
        if arr[lo] == arr[hi]:
            return lo if arr[lo] == x else -1

        # Estimate the target position
        pos = lo + ((x - arr[lo]) * (hi - lo)) // (arr[hi] - arr[lo])

        if arr[pos] == x:
            return pos

        # Search the appropriate half recursively
        if arr[pos] < x:
            return interpolationSearch(arr, pos + 1, hi, x)

        return interpolationSearch(arr, lo, pos - 1, x)

    return -1


if __name__ == "__main__":
    arr = [10, 12, 13, 16, 18, 19, 20, 21, 22,
           23, 24, 33, 35, 42, 47]
    x = 18

    index = interpolationSearch(arr, 0, len(arr) - 1, x)

    if index != -1:
        print("Element found at index", index)
    else:
        print("Element not found.")
C#
using System;

class GFG {

    // Recursive function to perform interpolation search
    static int interpolationSearch(int[] arr, int lo, int hi, int x) {
        if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
            // Handle equal boundary elements
            if (arr[lo] == arr[hi])
                return (arr[lo] == x) ? lo : -1;

            // Estimate the target position
            int pos = lo + (int)(((long)(x - arr[lo]) * (hi - lo)) /
                                 (arr[hi] - arr[lo]));

            if (arr[pos] == x)
                return pos;

            // Search the appropriate half recursively
            if (arr[pos] < x)
                return interpolationSearch(arr, pos + 1, hi, x);

            return interpolationSearch(arr, lo, pos - 1, x);
        }

        return -1;
    }

    public static void Main() {
        int[] arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                     23, 24, 33, 35, 42, 47};
        int x = 18;

        int index = interpolationSearch(arr, 0, arr.Length - 1, x);

        if (index != -1)
            Console.WriteLine("Element found at index " + index);
        else
            Console.WriteLine("Element not found.");
    }
}
JavaScript
// Recursive function to perform interpolation search
function interpolationSearch(arr, lo, hi, x) {
    if (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        // Handle equal boundary elements
        if (arr[lo] === arr[hi])
            return arr[lo] === x ? lo : -1;

        // Estimate the target position
        const pos = lo + Math.floor(
            ((x - arr[lo]) * (hi - lo)) / (arr[hi] - arr[lo])
        );

        if (arr[pos] === x)
            return pos;

        // Search the appropriate half recursively
        if (arr[pos] < x)
            return interpolationSearch(arr, pos + 1, hi, x);

        return interpolationSearch(arr, lo, pos - 1, x);
    }

    return -1;
}

// Driver code
function main() {
    const arr = [10, 12, 13, 16, 18, 19, 20, 21, 22,
                 23, 24, 33, 35, 42, 47];
    const x = 18;

    const index = interpolationSearch(arr, 0, arr.length - 1, x);

    if (index !== -1)
        console.log("Element found at index " + index);
    else
        console.log("Element not found.");
}

main();

Output
Element found at index 4
  • Time Complexity: O(log2(log2 n)) for the average case, and O(n) for the worst case 
  • Auxiliary Space Complexity: O(n)

Iterative Approach

  • Initialize lo = 0 and hi = n - 1.
  • While lo <= hi and x lies between arr[lo] and arr[hi], calculate the probable position pos using the interpolation formula.
  • If arr[pos] == x, return pos.
  • If arr[pos] < x, update lo = pos + 1; otherwise, update hi = pos - 1.
  • If the element is not found, return -1.
C++
#include <iostream>
#include <vector>
using namespace std;

// Iterative interpolation search
int interpolationSearch(vector<int>& arr, int x) {
    int lo = 0, hi = arr.size() - 1;

    while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        
        // Handle equal boundary elements
        if (arr[lo] == arr[hi])
            return (arr[lo] == x) ? lo : -1;

        // Estimate the target position
        int pos = lo + (int)(((long long)(x - arr[lo]) * (hi - lo)) /
                             (arr[hi] - arr[lo]));

        if (arr[pos] == x)
            return pos;

        // Update the search range
        if (arr[pos] < x)
            lo = pos + 1;
        else
            hi = pos - 1;
    }

    return -1;
}

int main() {
    vector<int> arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                       23, 24, 33, 35, 42, 47};
    int x = 18;

    int index = interpolationSearch(arr, x);

    if (index != -1)
        cout << "Element found at index " << index;
    else
        cout << "Element not found.";

    return 0;
}
C
#include <stdio.h>

// Iterative interpolation search
int interpolationSearch(int arr[], int n, int x) {
    int lo = 0, hi = n - 1;

    while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        
        // Handle equal boundary elements
        if (arr[lo] == arr[hi])
            return (arr[lo] == x) ? lo : -1;

        // Estimate the target position
        int pos = lo + (int)(((long long)(x - arr[lo]) * (hi - lo)) /
                             (arr[hi] - arr[lo]));

        if (arr[pos] == x)
            return pos;

        // Update the search range
        if (arr[pos] < x)
            lo = pos + 1;
        else
            hi = pos - 1;
    }

    return -1;
}

int main() {
    int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                 23, 24, 33, 35, 42, 47};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 18;

    int index = interpolationSearch(arr, n, x);

    if (index != -1)
        printf("Element found at index %d\n", index);
    else
        printf("Element not found.\n");

    return 0;
}
Java
class GFG {

    // Iterative interpolation search
    static int interpolationSearch(int[] arr, int x) {
        int lo = 0, hi = arr.length - 1;

        while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
            
            // Handle equal boundary elements
            if (arr[lo] == arr[hi])
                return (arr[lo] == x) ? lo : -1;

            // Estimate the target position
            int pos = lo + (int)(((long)(x - arr[lo]) * (hi - lo)) /
                                 (arr[hi] - arr[lo]));

            if (arr[pos] == x)
                return pos;

            // Update the search range
            if (arr[pos] < x)
                lo = pos + 1;
            else
                hi = pos - 1;
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                     23, 24, 33, 35, 42, 47};
        int x = 18;

        int index = interpolationSearch(arr, x);

        if (index != -1)
            System.out.println("Element found at index " + index);
        else
            System.out.println("Element not found.");
    }
}
Python
# Iterative interpolation search
def interpolationSearch(arr, x):
    lo, hi = 0, len(arr) - 1

    while lo <= hi and arr[lo] <= x <= arr[hi]:
        
        # Handle equal boundary elements
        if arr[lo] == arr[hi]:
            return lo if arr[lo] == x else -1

        # Estimate the target position
        pos = lo + ((x - arr[lo]) * (hi - lo)) // (arr[hi] - arr[lo])

        if arr[pos] == x:
            return pos

        # Update the search range
        if arr[pos] < x:
            lo = pos + 1
        else:
            hi = pos - 1

    return -1


if __name__ == "__main__":
    arr = [10, 12, 13, 16, 18, 19, 20, 21, 22,
           23, 24, 33, 35, 42, 47]
    x = 18

    index = interpolationSearch(arr, x)

    if index != -1:
        print("Element found at index", index)
    else:
        print("Element not found.")
C#
using System;

class GFG {

    // Iterative interpolation search
    static int interpolationSearch(int[] arr, int x) {
        int lo = 0, hi = arr.Length - 1;

        while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
            
            // Handle equal boundary elements
            if (arr[lo] == arr[hi])
                return (arr[lo] == x) ? lo : -1;

            // Estimate the target position
            int pos = lo + (int)(((long)(x - arr[lo]) * (hi - lo)) /
                                 (arr[hi] - arr[lo]));

            if (arr[pos] == x)
                return pos;

            // Update the search range
            if (arr[pos] < x)
                lo = pos + 1;
            else
                hi = pos - 1;
        }

        return -1;
    }

    public static void Main() {
        int[] arr = {10, 12, 13, 16, 18, 19, 20, 21, 22,
                     23, 24, 33, 35, 42, 47};
        int x = 18;

        int index = interpolationSearch(arr, x);

        if (index != -1)
            Console.WriteLine("Element found at index " + index);
        else
            Console.WriteLine("Element not found.");
    }
}
JavaScript
// Iterative interpolation search
function interpolationSearch(arr, x) {
    let lo = 0, hi = arr.length - 1;

    while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
        
        // Handle equal boundary elements
        if (arr[lo] === arr[hi])
            return arr[lo] === x ? lo : -1;

        // Estimate the target position
        const pos = lo + Math.floor(
            ((x - arr[lo]) * (hi - lo)) / (arr[hi] - arr[lo])
        );

        if (arr[pos] === x)
            return pos;

        // Update the search range
        if (arr[pos] < x)
            lo = pos + 1;
        else
            hi = pos - 1;
    }

    return -1;
}

// Driver code
function main() {
    const arr = [10, 12, 13, 16, 18, 19, 20, 21, 22,
                 23, 24, 33, 35, 42, 47];
    const x = 18;

    const index = interpolationSearch(arr, x);

    if (index !== -1)
        console.log("Element found at index " + index);
    else
        console.log("Element not found.");
}

main();

Output
Element found at index 4
  • Time Complexity: O(log2(log2 n)) for the average case, and O(n) for the worst case 
  • Auxiliary Space Complexity: O(1)
Comment