Skip to content

Commit e120c16

Browse files
authored
Merge pull request deutranium#100 from ankur-kayal/exponential-search
Exponential Search in C++ Added
2 parents 4fb518f + e5fb794 commit e120c16

File tree

2 files changed

+99
-23
lines changed

2 files changed

+99
-23
lines changed
Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
1-
21
# Exponential Search
32

4-
- Exponential search allows for searching through a sorted, unbounded list for a specified input value.
5-
- Time Complexity:
6-
- Worst Case Complexity:_O_(log _i_)
7-
- Best Case Complexity:_O_(1)
8-
- Average Case Complexity:_O_(log _i_)
3+
- Exponential search allows for searching through a sorted, unbounded list for a specified input value.
4+
- Time Complexity:
5+
- Worst Case Complexity:_O_(log _i_)
6+
- Best Case Complexity:_O_(1)
7+
- Average Case Complexity:_O_(log _i_)
98
- Worst Case Space Complexity:_O_(1)
10-
- Exponential Search will run in _O_(log _i_) time, where _i_ is the index of the element being searched for in the list, whereas binary search would run in _O_(log _n_) time, where _n_ is the number of elements in the list.
9+
- Exponential Search will run in _O_(log _i_) time, where _i_ is the index of the element being searched for in the list, whereas binary search would run in _O_(log _n_) time, where _n_ is the number of elements in the list.
1110
- Works only on sorted lists.
1211

1312
### Logic
1413

15-
1. Find range where element is present
14+
1. Find range where element is present
1615
2. Do Binary Search in above found range.
17-
3. Pseudo Code:
16+
3. Pseudo Code:
17+
18+
function exponentialSearch(arr, x, lo, hi):
19+
if arr[hi] >= x:
20+
return(binarySearch(arr, x, lo, hi + 1))
21+
else:
22+
return(exponentialSearch(arr, x, hi, hi * 2))
23+
24+
function binarySearch(arr, x, lo, hi):
25+
index = (lo + hi)/2
26+
if x = arr[index]
27+
return index
28+
else if x > arr[index]:
29+
return binarySearch(arr, x, index, hi)
30+
else:
31+
return binarySearch(arr, x, 0, index)
1832

19-
function exponentialSearch(arr, x, lo, hi):
20-
if arr[hi] >= x:
21-
return(binarySearch(arr, x, lo, hi + 1))
22-
else:
23-
return(exponentialSearch(arr, x, hi, hi * 2))
33+
### Implementations
2434

25-
function binarySearch(arr, x, lo, hi):
26-
index = (lo + hi)/2
27-
if x = arr[index]
28-
return index
29-
else if x > arr[index]:
30-
return binarySearch(arr, x, index, hi)
31-
else:
32-
return binarySearch(arr, x, 0, index)
35+
- [Python](exponentialSearch.py)
36+
- [C++](exponentialSearch.cpp)
3337

34-
### Instruction for Running code:
38+
### Instruction for Running code:
3539

3640
- Python
3741

3842
```
3943
python3 exponentialSearch.py
4044
```
45+
46+
- C++
47+
48+
For Windows
49+
50+
```
51+
g++ exponentialSearch.cpp -o exponentialSearch.exe
52+
exponentialSearch.exe
53+
```
54+
55+
For Linux/ Mac
56+
57+
```
58+
g++ exponentialSearch.cpp -o exponentialSearch.out
59+
./exponentialSearch.out
60+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// A recursive binary search function. It returns location of x in given array a[l..r] is present, otherwise -1
5+
int binarySearch(vector<int> &a, int l, int r, int x) {
6+
if (r >= l) {
7+
int mid = l + (r - l)/2;
8+
9+
// If the element is present at the middle itself
10+
if (a[mid] == x)
11+
return mid;
12+
13+
// If element is smaller than mid, then it can only be present in left subarray
14+
if (a[mid] > x)
15+
return binarySearch(a, l, mid-1, x);
16+
17+
// Else the element can only be present in right subarray
18+
return binarySearch(a, mid+1, r, x);
19+
}
20+
21+
// We reach here when element is not present in array
22+
return -1;
23+
}
24+
25+
// Returns position of first occurrence of x in array
26+
int exponentialSearch(vector<int> &a, int n, int x) {
27+
// If x is present at firt location itself
28+
if (a[0] == x)
29+
return 0;
30+
31+
// Find range for binary search by
32+
// repeated doubling
33+
int i = 1;
34+
while (i < n && a[i] <= x)
35+
i = i<<1;
36+
37+
// Call binary search for the found range.
38+
return binarySearch(a, i/2, min(i, n), x);
39+
}
40+
41+
42+
43+
44+
// Driver code
45+
int main() {
46+
vector<int> a = {2, 5, 6, 7, 9, 15, 23, 24, 89};
47+
int n = a.size();
48+
int x = 15;
49+
int result = exponentialSearch(a, n, x);
50+
if(result != -1) {
51+
cout << "\nElement found at index: " << result << '\n';
52+
}
53+
else {
54+
cout << "\nElement not found!\n";
55+
}
56+
}

0 commit comments

Comments
 (0)