-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExponentialSearch.cpp
More file actions
37 lines (31 loc) · 885 Bytes
/
ExponentialSearch.cpp
File metadata and controls
37 lines (31 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;
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;
}
int exponentialSearch(int arr[], int n, int key) {
if (arr[0] == key) return 0;
int i = 1;
while (i < n && arr[i] <= key)
i *= 2;
return binarySearch(arr, i / 2, min(i, n - 1), key);
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int index = exponentialSearch(arr, n, key);
if (index != -1)
cout << "Element found at index " << index;
else
cout << "Element not found";
}