JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

Index Search

Author:JIYIK Last Updated:2025/03/19 Views:

Exponential search, also known as doubling search or finger search, is an algorithm created for searching elements in large arrays. It is a two-step process. First, the algorithm tries to find the range in which the target element exists (L,R), and then uses binary search within this range to find the exact location of the target.

It is named exponential search because it finds the holding element in a range by searching pow(2,k)which element in index has the first index kgreater than the target element. Despite its name, the time complexity of this algorithm is logarithmic. It is very useful when the array is infinite in size and converges to a solution much faster than binary search.

Index Search Algorithm

Suppose we have an unsorted array A[]containing nelements and we want to find an element X.

  • Checks if the first element itself is the target element, ie A[0] == X.
  • Initialize i to 1.
  • When i < n and A[i] <= X, do the following.
    • Increment i by powers of 2, ie i=i*2.
  • Perform a binary search in the range i/2 to min(i,n-1).

Index Search Example

Suppose we have an array: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), and we want to find X - 10.

  1. Initialize i to 1.
  2. A[1] = 2 < 10, so increment i to 2.
  3. A[2] = 3 < 10, so increment i to 4.
  4. A[4] = 5 < 10, so increment i to 8.
  5. A[8] = 9 < 10, so increment i to 16.
  6. i = 16 > n Therefore binary search is called in the range of i/2, i.e. 8 to min(i,n-1), i.e. min(16,10) =10.
  7. Initialize lo to i/2 = 8 and hi to min(i,n-1) = 10.
  8. The calculated mid is 9.
  9. 10=10, that is, A[9] == X, so 9 is returned.

Implementation of the exponential search algorithm

#include <bits/stdc++.h>
using namespace std;

int binarySearch(int arr[], int lo, int hi, int x)
{
    while (lo <= hi) {
        int m = lo + (hi - lo) / 2;
        if (arr[m] == x)
            return m;
        if (arr[m] < x)
            lo = m + 1;
        else
            hi = m - 1;
    }
    return -1;
}

int exponentialSearch(int arr[], int n, int x)
{
    if (arr[0] == x)
        return 0;
    int i = 1;
    while (i < n && arr[i] <= x)
        i = i * 2;

    return binarySearch(arr, i / 2,
                        min(i, n - 1), x);
}

int main()
{
    int n = 11;
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    int x = 10;
    int result = exponentialSearch(arr, n, x);
    if (result == -1) {
        cout << "Element not found !!";
    }
    else cout << "Element found at index " << result;
    return 0;
}

Exponential search algorithm complexity

Time Complexity

  • Average situation

The average case time complexity is O(logi), where iis the index of the target element within the array X. It is even better than binary search when the element is close to the beginning of the array.

  • Best Case

The best case occurs when the element we are comparing is the element we are searching for and is returned in the first iteration. The time complexity of the best case is O(1).

  • Worst case scenario

The worst case time complexity is the same as the average case time complexity. The worst case time complexity is O(logi).

Space complexity

The space complexity of this algorithm is O(1), since it does not require any additional space except for the temporary variables.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Learning the Sorting Algorithm - Insertion Sort (Concepts)

Publish Date:2025/03/19 Views:95 Category:ALGORITHM

What is "insertion sort"? The concept is as follows: each time a record to be sorted is inserted into the previously sorted sequence according to its key size, until all records are inserted. Concepts are always somewhat abstract, and can a

Learning path of sorting algorithm - direct insertion sort

Publish Date:2025/03/19 Views:175 Category:ALGORITHM

This article follows up on Insertion Sort (Concepts) and presents the implementation steps and code for direct insertion sort. Since the Concepts section already has a large number of illustrations, it would be a bit long-winded to provide

Learning the sorting algorithm - Binary Insertion Sort

Publish Date:2025/03/19 Views:142 Category:ALGORITHM

This article follows the insertion sort (concept article) and presents the implementation steps and implementation code of the binary insertion sort Binary Insertion Sort Algorithm Steps Treat the first element of the first sequence to be s

The road to learning sorting algorithms - table insertion sort

Publish Date:2025/03/19 Views:193 Category:ALGORITHM

Table insertion sort was briefly mentioned in Insertion sort (concept) . I briefly summarized it and wrote this article. You can refer to it if you need it. Table insertion sort, as the name implies, uses an index table to sort the original

The road to learning sorting algorithms - Hill sort

Publish Date:2025/03/19 Views:50 Category:ALGORITHM

Hill sort is named after the designer of the algorithm, Hill. It is an improvement of Hill on the basis of insertion sort and can be said to be a special insertion sort. Here are the properties of insertion sort: First of all, the insertion

Things about the singleton design pattern

Publish Date:2025/03/19 Views:51 Category:ALGORITHM

The singleton design pattern is one of the most commonly used design patterns. The singleton design pattern, just by its name, you can roughly know its meaning. Single means one; instance means instance object. So a singleton has only one i

The road to learning sorting algorithms - merge sort

Publish Date:2025/03/19 Views:157 Category:ALGORITHM

Let's first look at the definition of merge sort Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. Merge the ordered subsequences to obt

The road to learning sorting algorithms - quick sort

Publish Date:2025/03/19 Views:121 Category:ALGORITHM

Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this is uncommon. In fact, quick sort is often significantly faste

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial