Interpolation Search in JavaScript

Interpolation search is an efficient searching algorithm for sorted arrays with uniformly distributed values. Unlike binary search, which always checks the middle element, interpolation search estimates where the target value is likely to be found based on its value relative to the array bounds.

How Interpolation Search Works

The algorithm uses a mathematical formula to estimate the position of the target element:

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

This formula returns a higher position when the target is closer to the end of the array, and a lower position when it's closer to the beginning.

Parameters

  • arr[] - Sorted array where elements need to be searched
  • target - Element to be searched
  • lo - Starting index in the array
  • hi - Ending index in the array

Implementation Example

const interpolationSearch = (arr = [], target) => {
    let left = 0;
    let right = arr.length - 1;
    
    while (left <= right) {
        const rangeDelta = arr[right] - arr[left];
        const indexDelta = right - left;
        const valueDelta = target - arr[left];
        
        // Target is out of range
        if (valueDelta < 0) {
            return -1;
        }
        
        // Handle case where all remaining elements are the same
        if (!rangeDelta) {
            return arr[left] === target ? left : -1;
        }
        
        // Calculate interpolated position
        const middleIndex = left + Math.floor((valueDelta * indexDelta) / rangeDelta);
        
        // Check if target found
        if (arr[middleIndex] === target) {
            return middleIndex;
        }
        
        // Narrow the search range
        if (arr[middleIndex] < target) {
            left = middleIndex + 1;
        } else {
            right = middleIndex - 1;
        }
    }
    
    return -1; // Target not found
};

// Test the algorithm
const arr = [1, 4, 6, 7, 9, 12, 15, 16, 17, 23, 25, 26, 27, 31];
const target = 25;

console.log(`Searching for ${target} in array:`, arr);
console.log(`Found at index:`, interpolationSearch(arr, target));
console.log(`Element at index 10:`, arr[10]);
Searching for 25 in array: [ 1, 4, 6, 7, 9, 12, 15, 16, 17, 23, 25, 26, 27, 31 ]
Found at index: 10
Element at index 10: 25

Multiple Search Examples

const arr = [2, 5, 8, 12, 16, 23, 38, 45, 56, 67, 78];

// Test different target values
const targets = [16, 2, 78, 99, 12];

targets.forEach(target => {
    const result = interpolationSearch(arr, target);
    if (result !== -1) {
        console.log(`Target ${target} found at index ${result}`);
    } else {
        console.log(`Target ${target} not found`);
    }
});
Target 16 found at index 4
Target 2 found at index 0
Target 78 found at index 10
Target 99 not found
Target 12 found at index 3

Time Complexity

Interpolation search has different time complexities depending on data distribution:

  • Best Case: O(1) - when the target is found immediately
  • Average Case: O(log log n) - for uniformly distributed data
  • Worst Case: O(n) - when data is not uniformly distributed

When to Use Interpolation Search

Interpolation search works best when:

  • Data is sorted and uniformly distributed
  • The array is large
  • Values follow a predictable pattern (like sequential numbers)

For irregularly distributed data, binary search might be more reliable.

Conclusion

Interpolation search is more efficient than binary search for uniformly distributed sorted arrays, offering O(log log n) average time complexity. However, it performs poorly with non-uniform data distribution, making binary search a safer general-purpose choice.

Updated on: 2026-03-15T23:19:00+05:30

758 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements