Perl Program to Implement Exponential Search

Perl Program to Implement Exponential Search

Exponential Search is a fast and efficient searching algorithm designed for sorted arrays. It works by finding a range where the target element may exist and then performing a more precise search, usually with Binary Search, within that range. This approach is particularly useful when searching through large datasets where starting with a full linear search would be inefficient. Exponential Search is beginner-friendly and introduces the idea of combining multiple search strategies to improve performance.

In real-world applications, Exponential Search is used in situations where data grows dynamically or when the position of the target element is likely to be near the beginning of the array. It is also handy in algorithm design and competitive programming, as it provides an intuitive way to quickly narrow down the search space before diving deeper with a precise search technique.

Program 1: Exponential Search with Binary Search

This program shows how Exponential Search finds the range for a target value and then uses Binary Search to locate it precisely.

use strict;
use warnings;

my @arr = (1, 3, 5, 7, 9, 12, 15, 18, 21, 24);
my $target = 15;

sub binary_search {

    my ($arr_ref, $low, $high, $target) = @_;
    while ($low <= $high) {

        my $mid = int(($low + $high) / 2);

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

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

    }

    return -1;

}

sub exponential_search {

    my ($arr_ref, $target) = @_;

    return 0 if $arr_ref->[0] == $target;
    my $i = 1;

    while ($i < scalar(@$arr_ref) && $arr_ref->[$i] <= $target) {
        $i *= 2;
    }

    return binary_search($arr_ref, int($i/2), $i < scalar(@$arr_ref) ? $i : $#{$arr_ref}, $target);

}

my $index = exponential_search(\@arr, $target);

print $index != -1 ? "Element $target found at index $index\n" : "Element $target not found\n";

In this program, Exponential Search quickly finds the approximate range for the target and then delegates the exact search to Binary Search. Beginners can see how combining algorithms can improve efficiency compared to using only linear or binary search alone.

Program 2: Recursive Exponential Search

This version uses recursion to perform Binary Search after determining the exponential range, emphasizing recursive logic.

use strict;
use warnings;

sub binary_search_recursive {

    my ($arr_ref, $low, $high, $target) = @_;
    return -1 if $low > $high;
    my $mid = int(($low + $high) / 2);

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

}

sub exponential_search_recursive {

    my ($arr_ref, $target) = @_;

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

    my $i = 1;

    while ($i < scalar(@$arr_ref) && $arr_ref->[$i] <= $target) {
        $i *= 2;
    }

    return binary_search_recursive($arr_ref, int($i/2), $i < scalar(@$arr_ref) ? $i : $#{$arr_ref}, $target);

}

my @arr = (2, 4, 6, 8, 10, 12, 14, 16);
my $target = 10;
my $index = exponential_search_recursive(\@arr, $target);

print $index != -1 ? "Element $target found at index $index\n" : "Element $target not found\n";

Recursive Exponential Search shows beginners how recursion can simplify the logic of searching within a subarray, helping to understand the divide-and-conquer approach.

Program 3: Exponential Search with Negative Numbers

This example demonstrates searching for negative values in a sorted array, showing the algorithm’s versatility.

use strict;
use warnings;

my @arr = (-40, -20, -10, 0, 5, 10, 20, 40);
my $target = -10;

sub binary_search {

    my ($arr_ref, $low, $high, $target) = @_;
    while ($low <= $high) {

        my $mid = int(($low + $high) / 2);

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

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

    }

    return -1;

}

sub exponential_search_neg {

    my ($arr_ref, $target) = @_;
    return 0 if $arr_ref->[0] == $target;
    my $i = 1;

    while ($i < scalar(@$arr_ref) && $arr_ref->[$i] <= $target) {
        $i *= 2;
    }

    return binary_search($arr_ref, int($i/2), $i < scalar(@$arr_ref) ? $i : $#{$arr_ref}, $target);

}

my $index = exponential_search_neg(\@arr, $target);

print $index != -1 ? "Element $target found at index $index\n" : "Element $target not found\n";

Here, the algorithm works seamlessly with negative numbers because the comparisons are handled in the same way. Beginners can understand that sorting is what makes Exponential Search possible, regardless of the numeric sign.

Program 4: Exponential Search with Floating-Point Numbers

This example searches through a sorted array of decimal numbers, highlighting its flexibility.

use strict;
use warnings;

my @arr = (0.1, 1.5, 2.2, 3.6, 4.8, 5.5);
my $target = 3.6;

sub binary_search {

    my ($arr_ref, $low, $high, $target) = @_;
    while ($low <= $high) {

        my $mid = int(($low + $high) / 2);

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

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

    }

    return -1;

}

sub exponential_search {

    my ($arr_ref, $target) = @_;

    return 0 if $arr_ref->[0] == $target;
    my $i = 1;

    while ($i < scalar(@$arr_ref) && $arr_ref->[$i] <= $target) {
        $i *= 2;
    }

    return binary_search($arr_ref, int($i/2), $i < scalar(@$arr_ref) ? $i : $#{$arr_ref}, $target);

}

my $index = exponential_search(\@arr, $target);
print $index != -1 ? "Element $target found at index $index\n" : "Element $target not found\n";

This demonstrates that Exponential Search can be applied to floating-point numbers in the same way as integers, as long as the array is sorted. Beginners see that the same algorithm can handle different types of numeric data.

Program 5: Encapsulated Exponential Search Subroutine

Encapsulating Exponential Search and Binary Search into reusable subroutines makes it easy to search multiple datasets.

use strict;
use warnings;

sub binary_search {

    my ($arr_ref, $low, $high, $target) = @_;
    while ($low <= $high) {

        my $mid = int(($low + $high) / 2);

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

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

    }

    return -1;

}

sub exponential_search_sub {

    my ($arr_ref, $target) = @_;

    my $binary_search = sub {

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

        while ($low <= $high) {

            my $mid = int(($low + $high)/2);
            return $mid if $arr_ref->[$mid] == $target;
            $low = $arr_ref->[$mid] < $target ? $mid + 1 : $low;
            $high = $arr_ref->[$mid] > $target ? $mid - 1 : $high;

        }

        return -1;

    };

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

    my $i = 1;

    while ($i < scalar(@$arr_ref) && $arr_ref->[$i] <= $target) {
        $i *= 2;
    }

    return $binary_search->($arr_ref, int($i/2), $i < scalar(@$arr_ref) ? $i : $#{$arr_ref}, $target);

}

my @arr = (1, 2, 4, 8, 16, 32, 64);
my $target = 16;

my $index = exponential_search_sub(\@arr, $target);
print $index != -1 ? "Element $target found at index $index\n" : "Element $target not found\n";

This subroutine approach encourages modular programming and allows beginners to quickly reuse the algorithm across different datasets without rewriting logic.

Frequently Asked Questions (FAQ)

Exponential Search raises common questions for beginners exploring efficient searching techniques.

Q1. What is the time complexity of Exponential Search?
The worst-case time complexity is O(log n), with the exponential phase quickly narrowing down the search range and Binary Search completing the precise search.

Q2. Can Exponential Search be used on unsorted arrays?
No, the array must be sorted; otherwise, the exponential phase cannot correctly find the range for the target element.

Q3. Is Exponential Search faster than Binary Search?
It can be faster if the target element is near the beginning of a large array since the exponential phase can locate the target’s range quickly.

Q4. Does Exponential Search handle negative and decimal numbers?
Yes, as long as the array is sorted, negative numbers and floating-point values are supported.

Q5. Do I need recursion for Exponential Search?
No, it can be implemented iteratively. Recursion is optional, mainly for the Binary Search phase.

Conclusion

Exponential Search is a versatile and efficient algorithm for searching sorted arrays, combining a rapid range-finding phase with a precise Binary Search. It is especially useful for large datasets or situations where the target element is likely near the start of the array.

For beginners, implementing Exponential Search in Perl is an excellent way to practice combining multiple algorithms, handling negative and floating-point numbers, and understanding both iterative and recursive approaches. By experimenting with the provided programs, learners can strengthen their grasp of search algorithms and be ready to explore more advanced algorithmic strategies in their coding journey.

Scroll to Top