C Program to Implement Interpolation Search

C Program to Implement Interpolation Search

Searching is one of the most common operations in computer programming. Whether you’re looking up a name in a contact list, a record in a database, or a specific value in an array, efficient searching saves both time and resources. Among the various search algorithms available, Interpolation Search stands out for its smart approach and improved performance on uniformly distributed data.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Interpolation Search is an advanced searching technique based on the idea of position estimation. Unlike Binary Search, which always looks in the middle of the current range, Interpolation Search tries to predict the likely position of the target value by considering how far it might be from the beginning or end of the range. This makes it faster in many cases, especially when dealing with sorted data that is evenly distributed, such as numeric ranges or ID lists. For beginners learning C programming, understanding and implementing this algorithm is a great step toward mastering efficient searching methods.

Program 1: Iterative Approach to Interpolation Search

The first version of our program implements Interpolation Search using loops, which makes it easy to follow and efficient in execution.

#include <stdio.h>

int interpolationSearch(int arr[], int n, int key) {

    int low = 0, high = n - 1;

    while (low <= high && key >= arr[low] && key <= arr[high]) {

        // Estimate the position of the key
        int pos = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]);

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

        if (arr[pos] < key)
            low = pos + 1;
        else
            high = pos - 1;

    }

    return -1;  // Key not found

}

int main() {

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

    printf("Enter the element to search: ");
    scanf("%d", &key);

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

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

    return 0;

}

In this program, the array is first assumed to be sorted. The interpolationSearch() function estimates the most likely position of the key using a mathematical formula that takes into account the value range of the array. If the key matches the element at that position, it returns the index. Otherwise, it narrows the search range based on whether the key is smaller or larger. This continues until the element is found or the range becomes invalid.

Beginners will find this version easy to grasp because it avoids recursion and uses a clear, step-by-step loop to handle the searching process. It also shows how interpolation can make searching faster when the data is evenly spaced.

Program 2: Recursive Implementation of Interpolation Search

Now let’s explore a recursive version of Interpolation Search. Recursion allows the function to call itself for smaller sections of the array until it finds the key or reaches the base condition.

#include <stdio.h>

int interpolationSearchRecursive(int arr[], int low, int high, int key) {

    if (low <= high && key >= arr[low] && key <= arr[high]) {

        int pos = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]);

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

        if (arr[pos] < key)
            return interpolationSearchRecursive(arr, pos + 1, high, key);

        return interpolationSearchRecursive(arr, low, pos - 1, key);

    }

    return -1;

}

int main() {

    int arr[] = {5, 15, 25, 35, 45, 55, 65, 75, 85};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key;

    printf("Enter the element to search: ");
    scanf("%d", &key);

    int index = interpolationSearchRecursive(arr, 0, n - 1, key);

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

    return 0;

}

This recursive implementation works in the same way as the iterative one but makes the logic a bit cleaner by reducing the number of variables handled within the function. The recursion continues until either the key is found or the search boundaries become invalid. For learners, recursion provides an elegant way to see how the algorithm divides the search space repeatedly, mirroring how we might mentally guess positions when searching through a range of numbers.

Program 3: Interpolation Search with Floating-Point Numbers

Interpolation Search isn’t limited to integers; it can also handle floating-point numbers as long as the data is sorted. This variation demonstrates how easily the algorithm can be adapted to handle different numeric data types.

#include <stdio.h>

int interpolationSearchFloat(float arr[], int n, float key) {

    int low = 0, high = n - 1;

    while (low <= high && key >= arr[low] && key <= arr[high]) {

        int pos = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]);

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

        if (arr[pos] < key)
            low = pos + 1;
        else
            high = pos - 1;

    }

    return -1;

}

int main() {

    float arr[] = {1.5, 3.2, 4.8, 6.0, 7.3, 8.9, 10.1, 12.4};
    int n = sizeof(arr) / sizeof(arr[0]);
    float key;

    printf("Enter the element to search: ");
    scanf("%f", &key);

    int index = interpolationSearchFloat(arr, n, key);

    if (index != -1)
        printf("Element %.2f found at index %d\n", key, index);
    else
        printf("Element %.2f not found in the array\n", key);

    return 0;

}

This program is nearly identical to the integer version, except it uses the float data type. The logic and steps remain the same. It’s a good reminder for beginners that algorithms are often flexible — by changing data types and conditions slightly, you can apply the same method to a wide variety of problems.

Frequently Asked Questions (FAQ)

Here are some of the most common questions students ask about Interpolation Search in C.

What is the time complexity of Interpolation Search?
In the best case, Interpolation Search can find an element in O(log log n) time, which is faster than Binary Search. However, in the worst case (when data isn’t uniformly distributed), it can take O(n) time.

Is Interpolation Search better than Binary Search?
Not always. It’s faster only when the data is evenly distributed. For irregular data, Binary Search is usually more consistent and reliable.

Can Interpolation Search work on unsorted arrays?
No, the array must be sorted before using Interpolation Search. The algorithm depends on value distribution to estimate positions accurately.

Does Interpolation Search work with strings?
Not directly. It’s designed for numeric data where arithmetic estimation can be applied. However, similar principles can be adapted for some special cases with numeric codes for characters.

Conclusion

Interpolation Search is a clever algorithm that estimates where an element might be rather than just dividing the array in half like Binary Search. This makes it particularly effective for uniformly distributed datasets, such as numeric ranges or sensor data.

By exploring both iterative and recursive implementations in C, you can clearly understand how the algorithm narrows down the search space intelligently. As you experiment more, try applying Interpolation Search to larger datasets or even compare its performance with Binary Search.

Scroll to Top