|
1 |
| - |
2 | 1 | # Exponential Search
|
3 | 2 |
|
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_) |
9 | 8 | - 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. |
11 | 10 | - Works only on sorted lists.
|
12 | 11 |
|
13 | 12 | ### Logic
|
14 | 13 |
|
15 |
| - 1. Find range where element is present |
| 14 | +1. Find range where element is present |
16 | 15 | 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) |
18 | 32 |
|
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 |
24 | 34 |
|
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) |
33 | 37 |
|
34 |
| -### Instruction for Running code: |
| 38 | +### Instruction for Running code: |
35 | 39 |
|
36 | 40 | - Python
|
37 | 41 |
|
38 | 42 | ```
|
39 | 43 | python3 exponentialSearch.py
|
40 | 44 | ```
|
| 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 | +``` |
0 commit comments