πŸ“˜ DSA Day 11 – Interpolation Search

πŸ“Œ Introduction

Interpolation Search is an improved version of Binary Search that works on sorted and uniformly distributed arrays.
Instead of always picking the middle index, it estimates where the target value might be, based on its value relative to the first and last elements.

Think of it like looking for a name in a telephone book β€” if the name starts with β€œZ,” you don’t start in the middle, you jump near the end.


πŸ”Ή How It Works

  1. Start with the low and high indices (low = 0, high = n-1).

  2. Use the interpolation formula to estimate the probable position:

  3. Check:

    • If arr[pos] equals target β†’ found.

    • If arr[pos] is less than target β†’ search right side (low = pos + 1).

    • If arr[pos] is greater than target β†’ search left side (high = pos - 1).

  4. Repeat until found or range becomes invalid.


πŸ“Š Example

Search for 33 in:
[10, 20, 30, 40, 50, 60, 70, 80, 90]

  • Low = 0, High = 8

  • Estimated position:

    Index 2 = 30 β†’ less than 33 β†’ shift low to 3.

  • Repeat process β†’ found at position 3.


πŸ’» Pseudocode

function interpolationSearch(arr, target):
    low = 0
    high = length(arr) - 1

    while low <= high and target >= arr[low] and target <= arr[high]:
        pos = low + ((target - arr[low]) * (high - low)) / (arr[high] - arr[low])

        if arr[pos] == target:
            return pos
        if arr[pos] < target:
            low = pos + 1
        else:
            high = pos - 1

    return -1

πŸ’» JavaScript Implementation

function interpolationSearch(arr, target) {
    let low = 0, high = arr.length - 1;

    while (low <= high && target >= arr[low] && target <= arr[high]) {
        if (low === high) {
            return arr[low] === target ? low : -1;
        }

        let pos = low + Math.floor(
            ((target - arr[low]) * (high - low)) / (arr[high] - arr[low])
        );

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

    return -1;
}

// Example usage
let arr = [10, 20, 30, 40, 50, 60, 70, 80, 90];
console.log(interpolationSearch(arr, 30)); // Output: 2

βš– Time Complexity

  • Best case: O(1)

  • Average case: O(log log n) β€” faster than Binary Search for uniform data.

  • Worst case: O(n) β€” if data is not uniformly distributed.

  • Space Complexity: O(1)


πŸ“ Key Points

  • Works only for sorted data.

  • Best performance on uniformly distributed data sets.

  • Not great for skewed or clustered data.


βœ… Tomorrow (Day 12) β†’ We’ll cover Exponential Search.

#CodeWithGift #DSA2025 #JavaScript #DayEleven

1
Subscribe to my newsletter

Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

God'sgift Samuel
God'sgift Samuel

About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.