C Program to Implement Exponential Search

C Program to Implement Exponential Search

Searching for data efficiently is one of the most important operations in computer science. When working with sorted data, we often use algorithms like Binary Search because it reduces the search space quickly. However, when the size of the array is large or the position of the searched element is unknown, Exponential Search becomes an even smarter choice.

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 works by first finding the range where the element might exist and then applying Binary Search within that range. It grows the search interval exponentially — doubling it every step — which allows it to reach close to the target quickly. This makes it ideal for scenarios where data is sorted but we don’t know the bounds or size upfront, such as searching in unbounded or dynamically growing arrays.

For beginners learning C programming, Exponential Search is an excellent way to understand how multiple algorithms can work together — here, range detection using exponential growth and Binary Search for precision.

Program 1: Exponential Search using Iteration

This first program demonstrates Exponential Search using an iterative approach. It combines exponential range finding with a traditional Binary Search to locate the target element.

#include <stdio.h>

// Function to perform binary search
int binarySearch(int arr[], int left, int right, int key) {

    while (left <= right) {

        int mid = left + (right - left) / 2;

        if (arr[mid] == key)
            return mid;
        else if (arr[mid] < key)
            left = mid + 1;
        else
            right = mid - 1;

    }

    return -1;

}

// Function to perform exponential search
int exponentialSearch(int arr[], int n, int key) {

    if (arr[0] == key)
        return 0;

    int i = 1;

    while (i < n && arr[i] <= key)
        i = i * 2;

    int left = i / 2;
    int right = (i < n) ? i : n - 1;

    return binarySearch(arr, left, right, key);

}

int main() {

    int arr[] = {2, 4, 6, 10, 18, 25, 30, 40, 50, 60};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key;

    printf("Enter the element to search: ");
    scanf("%d", &key);

    int result = exponentialSearch(arr, n, key);

    if (result != -1)
        printf("Element %d found at index %d\n", key, result);
    else
        printf("Element %d not found in the array\n", key);

    return 0;

}

In this program, the algorithm starts by checking the first element. If it’s not the key, the range is doubled at each step — 1, 2, 4, 8, and so on — until the value at that index exceeds the key or the array boundary is reached. Once the correct range is identified, Binary Search is used to find the exact position of the key.

This method is efficient for sorted arrays and helps beginners understand how combining algorithms can improve search performance. The iterative version also makes it easier to visualize how the search range expands exponentially before narrowing down.

Program 2: Exponential Search using Recursion

The recursive version of Exponential Search works similarly but uses recursion both in finding the range and in the Binary Search step.

#include <stdio.h>

// Recursive Binary Search
int binarySearchRecursive(int arr[], int left, int right, int key) {

    if (left > right)
        return -1;

    int mid = left + (right - left) / 2;

    if (arr[mid] == key)
        return mid;
    else if (arr[mid] < key)
        return binarySearchRecursive(arr, mid + 1, right, key);
    else
        return binarySearchRecursive(arr, left, mid - 1, key);

}

// Recursive Exponential Search
int exponentialSearchRecursive(int arr[], int n, int i, int key) {

    if (i >= n || arr[i] > key) {

        int left = i / 2;
        int right = (i < n) ? i : n - 1;
        return binarySearchRecursive(arr, left, right, key);

    }

    return exponentialSearchRecursive(arr, n, i * 2, key);

}

int main() {

    int arr[] = {3, 7, 12, 19, 24, 32, 41, 50, 61, 73};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key;

    printf("Enter the element to search: ");
    scanf("%d", &key);

    if (arr[0] == key)

        printf("Element %d found at index 0\n", key);

    else {

        int index = exponentialSearchRecursive(arr, n, 1, key);

        if (index != -1)
            printf("Element %d found at index %d\n", key, index);
        else
            printf("Element %d not found in the array\n", key);

    }

    return 0;

}

The recursive implementation breaks the logic into smaller, more understandable chunks. First, it recursively doubles the index until it finds the possible range. Then it calls binarySearchRecursive() to locate the key precisely. This version is elegant and great for understanding how recursion simplifies iterative loops, making the flow of the algorithm easier to read for those familiar with recursive programming in C.

Program 3: Exponential Search with Floating-Point Numbers

Exponential Search can also be used with floating-point numbers, as long as the array is sorted. This flexibility makes it suitable for real-world data where values aren’t always integers.

#include <stdio.h>

int binarySearchFloat(float arr[], int left, int right, float key) {

    while (left <= right) {

        int mid = left + (right - left) / 2;

        if (arr[mid] == key)
            return mid;
        else if (arr[mid] < key)
            left = mid + 1;
        else
            right = mid - 1;

    }

    return -1;

}

int exponentialSearchFloat(float arr[], int n, float key) {

    if (arr[0] == key)
        return 0;

    int i = 1;

    while (i < n && arr[i] <= key)
        i *= 2;

    int left = i / 2;
    int right = (i < n) ? i : n - 1;

    return binarySearchFloat(arr, left, right, key);

}

int main() {

    float arr[] = {1.2, 2.5, 3.8, 4.9, 6.1, 7.5, 8.9, 10.2};
    int n = sizeof(arr) / sizeof(arr[0]);
    float key;

    printf("Enter the element to search: ");
    scanf("%f", &key);

    int result = exponentialSearchFloat(arr, n, key);

    if (result != -1)
        printf("Element %.2f found at index %d\n", key, result);
    else
        printf("Element %.2f not found in the array\n", key);

    return 0;

}

This example behaves just like the integer version, but works with decimal values. Beginners will appreciate how easily Exponential Search adapts to different data types as long as the dataset remains sorted. It’s a good demonstration of algorithm versatility in C programming.

Frequently Asked Questions (FAQ)

Here are some common questions learners ask about Exponential Search:

What is the time complexity of Exponential Search?
The time complexity is O(log n), the same as Binary Search. The range-finding step grows exponentially, but the final Binary Search remains logarithmic in nature.

Is Exponential Search better than Binary Search?
It depends. Exponential Search performs better when the target element is near the beginning of the array or when the array size is unknown. For fixed-size arrays, both perform similarly.

Does Exponential Search require sorted data?
Yes, the array must be sorted in ascending order for the algorithm to work correctly.

Can Exponential Search handle floating-point numbers?
Yes, as long as the array is sorted, it works perfectly with both integer and floating-point values.

Where is Exponential Search used?
It’s often used in unbounded arrays, large data structures, or cases where data arrives dynamically and the size isn’t known in advance.

Conclusion

Exponential Search is a clever combination of two powerful ideas: exponential range expansion and binary searching within that range. It efficiently finds elements in sorted data, especially when the size or limits of the dataset are unknown.

By exploring both the iterative and recursive approaches, beginners can develop a deep understanding of how to combine algorithms and make them more efficient. Whether you’re working with integers or floating-point numbers, Exponential Search demonstrates the power of divide-and-conquer in C programming.

Scroll to Top