Exponential Search

Exponential search is also known as doubling or galloping search. This mechanism is used to find the range where the search key may present. If L and U are the upper and lower bound of the list, then L and U both are the power of 2. For the last section, the U is the last position of the list. For that reason, it is known as exponential.

After finding the specific range, it uses the binary search technique to find the exact location of the search key.

The complexity of Exponential Search Technique

  • Time Complexity: O(1) for the best case. O(log2 i) for average or worst case. Where i is the location where search key is present.
  • Space Complexity: O(1)

Input and Output

Input:
A sorted list of data:
10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956 995
The search key 780
Output:
Item found at location: 16

Algorithm

binarySearch(array, start, end, key)

Input: An sorted array, start and end location, and the search key

Output − location of the key (if found), otherwise wrong location.

Begin
   if start  key then
         call binarySearch(array, mid+1, end, key)
      else when array[mid] 

exponentialSearch(array, start, end, key)

Input: An sorted array, start and end location, and the search key

Output: location of the key (if found), otherwise wrong location.

Begin
   if (end – start) 

Example

#include
using namespace std;

int binarySearch(int array[], int start, int end, int key) {
   if(start  key)
         return binarySearch(array, start, mid-1, key);
         return binarySearch(array, mid+1, end, key);
   }
   return -1;
}

int exponentialSearch(int array[], int start, int end, int key){
   if((end - start) > n;
   int arr[n]; //create an array of size n
   cout > arr[i];
   }
   cout > searchKey;
   if((loc = exponentialSearch(arr, 0, n, searchKey)) >= 0)
      cout 

Output

Enter number of items: 20
Enter items:
10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956 995
Enter search key to search in the list: 780
Item found at location: 16
Updated on: 2020-06-15T14:10:42+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements