Perl Program to Implement Interpolation Search

Perl Program to Implement Interpolation Search

Interpolation Search is a smart searching algorithm designed for sorted and uniformly distributed arrays. Unlike Binary Search, which splits the array in half each time, Interpolation Search estimates the position of the target based on the values at the boundaries. This can make it faster than Binary Search for certain types of data, especially when the values are evenly spaced. Understanding this algorithm gives beginners insight into how math can improve search efficiency beyond just dividing arrays in half.

In practical terms, Interpolation Search is often used in applications like searching through phone directories, ID numbers, or any data where values are spread uniformly. For beginners in Perl, this algorithm is a great way to learn both the importance of data ordering and how to combine arithmetic calculations with conditional logic to solve real-world problems efficiently.

Program 1: Iterative Interpolation Search

This program demonstrates Interpolation Search using a loop to find a target value in a sorted array of numbers.

use strict;
use warnings;

my @arr = (10, 20, 30, 40, 50, 60, 70);
my $target = 50;
my ($low, $high) = (0, $#arr);
my $found_index = -1;

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

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

    if ($arr[$pos] == $target) {
        $found_index = $pos;
        last;
    } elsif ($arr[$pos] < $target) {
        $low = $pos + 1;
    } else {
        $high = $pos - 1;
    }

}

if ($found_index != -1) {
    print "Element $target found at index $found_index\n";
} else {
    print "Element $target not found\n";
}

In this program, the algorithm calculates an estimated position using the values of the first and last elements. Beginners can see how this prediction allows the search to skip over large parts of the array, potentially improving speed compared to Linear or Binary Search.

Program 2: Recursive Interpolation Search

Interpolation Search can also be implemented recursively, showing a clean and elegant approach to the same logic.

use strict;
use warnings;

sub interpolation_search {

    my ($arr_ref, $target, $low, $high) = @_;
    return -1 if $low > $high || $target < $arr_ref->[$low] || $target > $arr_ref->[$high];

    my $pos = $low + int((($high - $low) * ($target - $arr_ref->[$low])) / ($arr_ref->[$high] - $arr_ref->[$low]));

    return $pos if $arr_ref->[$pos] == $target;
    return interpolation_search($arr_ref, $target, $pos + 1, $high) if $arr_ref->[$pos] < $target;
    return interpolation_search($arr_ref, $target, $low, $pos - 1);

}

my @arr = (5, 15, 25, 35, 45, 55);
my $target = 35;
my $index = interpolation_search(\@arr, $target, 0, $#arr);

if ($index != -1) {
    print "Element $target found at index $index\n";
} else {
    print "Element $target not found\n";
}

The recursive version repeatedly estimates the position and narrows the search space, giving beginners a clear example of how recursion simplifies repetitive logic while keeping the algorithm efficient.

Program 3: Interpolation Search with a subroutine

By encapsulating the search logic into a subroutine, this program makes the algorithm reusable and easy to test with different arrays and target values.

use strict;
use warnings;

sub interpolation_search {

    my ($arr_ref, $target) = @_;
    my ($low, $high) = (0, $#$arr_ref);

    while ($low <= $high && $target >= $arr_ref->[$low] && $target <= $arr_ref->[$high]) {

        my $pos = $low + int((($high - $low) * ($target - $arr_ref->[$low])) / ($arr_ref->[$high] - $arr_ref->[$low]));

        return $pos if $arr_ref->[$pos] == $target;

        if ($arr_ref->[$pos] < $target) {
            $low = $pos + 1;
        } else {
            $high = $pos - 1;
        }

    }

    return -1;

}

my @arr = (1, 2, 3, 4, 5, 6, 7, 8);
my $target = 6;
my $result = interpolation_search(\@arr, $target);

if ($result != -1) {
    print "Element $target found at index $result\n";
} else {
    print "Element $target not found\n";
}

Using a subroutine helps beginners understand modular programming and makes it easy to plug this search logic into larger Perl projects without rewriting it each time.

Program 4: Interpolation Search with negative numbers

This example shows that Interpolation Search can handle negative numbers as long as the array remains sorted.

use strict;
use warnings;

my @arr = (-50, -20, -10, 0, 10, 20, 30);
my $target = -20;
my ($low, $high) = (0, $#arr);
my $found_index = -1;

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

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

    if ($arr[$pos] == $target) {
        $found_index = $pos;
        last;
    } elsif ($arr[$pos] < $target) {
        $low = $pos + 1;
    } else {
        $high = $pos - 1;
    }

}

if ($found_index != -1) {
    print "Element $target found at index $found_index\n";
} else {
    print "Element $target not found\n";
}

Beginners can see that negative values do not affect the arithmetic calculations of Interpolation Search, as it relies on relative differences rather than absolute values.

Program 5: Interpolation Search with floating-point numbers

Interpolation Search can also work with decimal values, as this program demonstrates.

use strict;
use warnings;

my @arr = (0.5, 1.2, 2.4, 3.6, 4.8, 6.0);
my $target = 3.6;
my ($low, $high) = (0, $#arr);
my $found_index = -1;

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

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

    if ($arr[$pos] == $target) {
        $found_index = $pos;
        last;
    } elsif ($arr[$pos] < $target) {
        $low = $pos + 1;
    } else {
        $high = $pos - 1;
    }

}

if ($found_index != -1) {
    print "Element $target found at index $found_index\n";
} else {
    print "Element $target not found\n";
}

This example shows that Interpolation Search works naturally with floating-point numbers, making it versatile for numeric datasets in Perl programs.

Frequently Asked Questions (FAQ)

This section addresses common beginner questions about Interpolation Search.

Q1. What is the time complexity of Interpolation Search?
For uniformly distributed arrays, the average time complexity is O(log log n), which can be faster than Binary Search. In the worst case, it is O(n).

Q2. Can Interpolation Search work with unsorted arrays?
No, the array must be sorted for Interpolation Search to work correctly.

Q3. How is Interpolation Search different from Binary Search?
Binary Search splits the array in half every time, while Interpolation Search estimates the likely position based on the target value and array distribution.

Q4. Can it handle negative and decimal numbers?
Yes, as long as the array is sorted, Interpolation Search works with any numeric values, including negatives and floating-point numbers.

Q5. When should I use Interpolation Search over Binary Search?
Use it when the array is sorted and values are uniformly distributed, as it can be more efficient in such cases.

Conclusion

Interpolation Search is a clever and efficient algorithm for searching sorted and uniformly distributed arrays. By estimating the position of the target value rather than splitting the array in half, it can outperform Binary Search in certain situations. The examples in this article show iterative, recursive, and subroutine-based approaches, and demonstrate that the algorithm works with negative and floating-point numbers as well.

Beginners practicing these programs in Perl will not only learn how Interpolation Search works but also develop a stronger understanding of algorithmic thinking, data ordering, and the importance of efficient searching. By exploring these techniques, you gain the foundation to tackle more advanced searches and real-world data problems with confidence.

Scroll to Top