Binary Search vs. Interpolation Search

Nishant ChauhanNishant Chauhan
5 min read

When it comes to searching for an element in a sorted array, two popular algorithms often come up: Binary Search and Interpolation Search. While both are efficient compared to linear search, they work in fundamentally different ways. Let’s break them down in simple terms and compare their strengths and weaknesses.

How Binary Search Works

Binary Search follows a divide-and-conquer strategy. It repeatedly cuts the search space in half until it finds the target element or determines that it’s not present.

Steps:

  1. Start with the middle element of the array.

  2. If it matches the target, return the index.

  3. If the target is smaller, repeat the search in the left half.

  4. If the target is larger, repeat the search in the right half.

  5. Continue until the element is found or the search space becomes empty.

Example:

Scenario: You have a sorted list of numbers [10, 20, 30, 40, 50, 60, 70], and you want to find 40.

Formula: The middle index is calculated as:

$$\text{mid} = \frac{\text{low} + \text{high}}{2}$$

It divides the array into halves and searches in the appropriate half.

How It Works:

  1. It starts with low = 0 and high = len(arr) - 1.

  2. It finds mid = (0 + 6) // 2 = 3.

  3. Since arr[3] = 40 matches the target, it returns index 3.

Binary Search Code (Java) :

class BinarySearch {

    public static int binarySearch(int[] arr, int target) {
        int low = 0, high = arr.length - 1;

        while (low <= high) {
            int mid = (low + high) / 2; 

            if (arr[mid] == target)
                return mid; 
            else if (arr[mid] < target)
                low = mid + 1; 
            else
                high = mid - 1; 
        }
        return -1; 
    }

    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50, 60, 70};
        int target = 40;
        int index = binarySearch(arr, target);

        if (index != -1)
            System.out.println("Element found at index: " + index);
        else
            System.out.println("Element not found");
    }
}

Time Complexity:

  • Best Case: O(1) (if the middle element is the target)

  • Average and Worst Case: O(log n)

When to Use:

  • When the array is sorted.

  • When the data is evenly distributed.

  • When fast searching is required with predictable performance.

How Interpolation Search Works

Interpolation Search is an optimized version of Binary Search but works best when values in the array are uniformly distributed. Instead of jumping to the middle, it estimates the probable position of the target based on its value.

Steps:

  1. Use the formula to estimate the probable index:

Example :

Scenario: You have a uniformly distributed sorted list [10, 20, 30, 40, 50, 60, 70], and you want to find 50.

Formula: Instead of jumping to the middle, we estimate the position using:

$$\text{pos} = \text{low} + \frac{(\text{target} - \text{arr} [ \text{low}]) \times (\text{high} - \text{low})}{\text{arr}[\text{high}] - \text{arr}[\text{low}]}$$

It estimates the likely position of the target instead of jumping to the middle.

How It Works:

  1. It estimates pos using the formula.

  2. If arr[pos] == target, it returns the index.

  3. If arr[pos] < target, it moves right; otherwise, it moves left.

Interpolation Search Code (Java) :

class InterpolationSearch {

    public static int interpolationSearch(int[] arr, int target) {
        int low = 0, high = arr.length - 1;

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

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

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

    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50, 60, 70};
        int target = 50;
        int index = interpolationSearch(arr, target);

        if (index != -1)
            System.out.println("Element found at index: " + index);
        else
            System.out.println("Element not found");
    }
}

Time Complexity:

  • Best Case: O(1) (if the estimate is perfect)

  • Average Case: O(log log n) (better than Binary Search for uniformly distributed data)

  • Worst Case: O(n) (if data is highly skewed or non-uniform)

When to Use:

  • When the array is sorted and uniformly distributed.

  • When working with large datasets where estimated jumps can save time.

  • When searching for values in datasets like temperature readings or student roll numbers.

Key Differences

FeatureBinary SearchInterpolation Search
Search MethodMiddle elementEstimated position based on value
Best PerformanceO(1)O(1)
Average CaseO(log n)O(log log n)
Worst CaseO(log n)O(n)
Requires Sorted DataYesYes
Works on Uniform DataYesBest suited
Works on Skewed DataYesPoor performance

Which One Should You Use?

  • Use Binary Search when the data is sorted but not necessarily uniform.

  • Use Interpolation Search when the data is both sorted and uniformly distributed.

  • If unsure, Binary Search is a safer choice due to its more predictable performance.

Conclusion

Both algorithms improve search efficiency significantly over linear search. Binary Search is more universally applicable, while Interpolation Search shines in specialized cases. Understanding when to use each will help you optimize your searches effectively!

0
Subscribe to my newsletter

Read articles from Nishant Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nishant Chauhan
Nishant Chauhan