VB .NET Program to Implement Exponential Search

VB .NET Program to Implement Exponential Search

Searching efficiently is a key part of programming, especially when working with large datasets. While Binary Search is a classic method for finding elements in a sorted array, Exponential Search offers another powerful approach. It is particularly useful when the element we are searching for is located near the beginning of a sorted array. Exponential Search works by first finding a range where the target might exist and then performing a Binary Search within that range. This combination makes it fast and efficient.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Exponential Search is widely used in real-world applications where the data is sorted or nearly sorted. For example, it can be used in searching through sorted log files, large databases, or even arrays with dynamically growing sizes. Learning this algorithm helps beginners understand how combining search strategies can improve performance and introduces the concept of exponential growth in algorithms.

Program 1: Basic Exponential Search for Integers

This program shows the classic Exponential Search using a simple sorted integer array. It combines exponential range finding with binary search.

Module Module1

    Function BinarySearch(arr() As Integer, target As Integer, left As Integer, right As Integer) As Integer

        While left <= right

            Dim mid As Integer = left + (right - left) \ 2

            If arr(mid) = target Then Return mid

            If arr(mid) < target Then
                left = mid + 1
            Else
                right = mid - 1
            End If

        End While

        Return -1

    End Function

    Function ExponentialSearch(arr() As Integer, target As Integer) As Integer

        If arr(0) = target Then Return 0
        Dim i As Integer = 1

        While i < arr.Length AndAlso arr(i) <= target
            i *= 2
        End While

        Return BinarySearch(arr, target, i \ 2, Math.Min(i, arr.Length - 1))

    End Function

    Sub Main()

        Dim data() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
        Dim target As Integer = 11
        Dim result As Integer = ExponentialSearch(data, target)

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

The program first doubles the index until it finds a number greater than or equal to the target. Then it runs Binary Search in that range. Beginners can see how combining exponential growth and binary search allows quick location of elements.

Program 2: Exponential Search with Negative Numbers

This version handles negative integers, showing that the algorithm works with all signed numbers in a sorted array.

Module Module1

    Function BinarySearch(arr() As Integer, target As Integer, left As Integer, right As Integer) As Integer

        While left <= right

            Dim mid As Integer = left + (right - left) \ 2

            If arr(mid) = target Then Return mid

            If arr(mid) < target Then
                left = mid + 1
            Else
                right = mid - 1
            End If

        End While

        Return -1

    End Function

    Function ExponentialSearch(arr() As Integer, target As Integer) As Integer

        If arr(0) = target Then Return 0

        Dim i As Integer = 1

        While i < arr.Length AndAlso arr(i) <= target
            i *= 2
        End While

        Return BinarySearch(arr, target, i \ 2, Math.Min(i, arr.Length - 1))

    End Function

    Sub Main()

        Dim data() As Integer = {-50, -20, -10, 0, 10, 20, 30}
        Dim target As Integer = -10
        Dim result As Integer = ExponentialSearch(data, target)

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

The method works just as well with negative numbers because it relies on comparisons, not absolute values. Beginners learn that sorted order is the key requirement.

Program 3: Exponential Search for Floating-Point Numbers

This program shows how Exponential Search can handle decimal values in a sorted array.

Module Module1

    Function BinarySearch(arr() As Double, target As Double, left As Integer, right As Integer) As Integer

        While left <= right

            Dim mid As Integer = left + (right - left) \ 2

            If arr(mid) = target Then Return mid

            If arr(mid) < target Then
                left = mid + 1
            Else
                right = mid - 1
            End If

        End While

        Return -1

    End Function

    Function ExponentialSearch(arr() As Double, target As Double) As Integer

        If arr(0) = target Then Return 0

        Dim i As Integer = 1

        While i < arr.Length AndAlso arr(i) <= target
            i *= 2
        End While

        Return BinarySearch(arr, target, i \ 2, Math.Min(i, arr.Length - 1))

    End Function

    Sub Main()

        Dim data() As Double = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0}
        Dim target As Double = 2.5
        Dim result As Integer = ExponentialSearch(data, target)

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

The approach works with decimal numbers just like integers. Beginners can see that type flexibility allows the same logic to be applied to integers, negatives, and floating-point numbers.

Program 4: Interactive Exponential Search

This version allows the user to input a sorted array and target value, making it practical for experimentation.

Module Module1

    Function BinarySearch(arr() As Integer, target As Integer, left As Integer, right As Integer) As Integer

        While left <= right

            Dim mid As Integer = left + (right - left) \ 2

            If arr(mid) = target Then Return mid

            If arr(mid) < target Then
                left = mid + 1
            Else
                right = mid - 1
            End If

        End While

        Return -1

    End Function

    Function ExponentialSearch(arr() As Integer, target As Integer) As Integer

        If arr(0) = target Then Return 0

        Dim i As Integer = 1

        While i < arr.Length AndAlso arr(i) <= target
            i *= 2
        End While

        Return BinarySearch(arr, target, i \ 2, Math.Min(i, arr.Length - 1))

    End Function

    Sub Main()

        Console.WriteLine("Enter sorted numbers separated by space:")

        Dim input As String = Console.ReadLine()
        Dim data() As Integer = Array.ConvertAll(input.Split(" "c), Function(s) Int32.Parse(s))

        Console.WriteLine("Enter number to search:")

        Dim target As Integer = Int32.Parse(Console.ReadLine())

        Dim result As Integer = ExponentialSearch(data, target)

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

This version is interactive and helps beginners understand how Exponential Search can be applied to dynamic user input. It highlights practical usage and testing of the algorithm.

Program 5: Exponential Search Using Recursion

This recursive version demonstrates a slightly different approach to structure the search.

Module Module1

    Function BinarySearch(arr() As Integer, target As Integer, left As Integer, right As Integer) As Integer

        If left > right Then Return -1

        Dim mid As Integer = left + (right - left) \ 2

        If arr(mid) = target Then Return mid

        If arr(mid) < target Then
            Return BinarySearch(arr, target, mid + 1, right)
        Else
            Return BinarySearch(arr, target, left, mid - 1)
        End If

    End Function

    Function ExponentialSearch(arr() As Integer, target As Integer, index As Integer) As Integer

        If index >= arr.Length OrElse arr(index) > target Then
            Return BinarySearch(arr, target, index \ 2, Math.Min(index, arr.Length - 1))
        End If

        Return ExponentialSearch(arr, target, index * 2)

    End Function

    Sub Main()

        Dim data() As Integer = {2, 4, 6, 8, 10, 12, 14}
        Dim target As Integer = 10
        Dim result As Integer = ExponentialSearch(data, target, 1)

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

Recursion elegantly expresses the exponential growth phase, while Binary Search handles the final step. Beginners can understand how recursive thinking complements iterative methods.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about Exponential Search:

Q1: What is the main advantage of Exponential Search?
It quickly finds a range where the target may exist, which is especially useful if the target is near the beginning of a large sorted array.

Q2: Does Exponential Search work with negative numbers or decimals?
Yes, as long as the array is sorted, Exponential Search works for all numeric types.

Q3: How does Exponential Search differ from Binary Search?
Exponential Search first finds a range using exponential jumps and then applies Binary Search. Binary Search divides the full array from the start.

Q4: Is Exponential Search faster than Binary Search?
It can be faster when the target is near the start of the array but generally has similar performance for evenly distributed data.

Q5: What are the key requirements for Exponential Search?
The array must be sorted, and the algorithm assumes random access to array elements.

Conclusion

Exponential Search is a powerful and efficient search algorithm that combines exponential range-finding with Binary Search. It works with integers, negative numbers, and floating-point numbers. By exploring iterative, recursive, interactive, and dynamic versions, beginners can understand how divide-and-conquer strategies improve performance in sorted datasets. Practicing Exponential Search in VB .NET strengthens problem-solving skills and prepares programmers for more advanced search algorithms.

Scroll to Top