How Exponential Search Works
index 016 of 16 still possible15
- Ruled out
- Still possible
- Checked now
- Found
1/10
Looking for 158 in 16 sorted values, from 4 to 196.
Settings
More options
Is it in the array?
Gaps between values
Two to 64 numbers. Sorted ascending before the search runs.
Exponential Search
- 1
if a[0] == target: return 0 - 2
- 3
// double the bound until it passes - 4
bound = 1 - 5
while bound < n and a[bound] < target: - 6
bound *= 2 - 7
- 8
// then binary search that span - 9
binarySearch(bound / 2, min(bound, n-1))
How the Six Compare
| Algorithm | Best | Average | Worst | Needs a sorted array |
|---|---|---|---|---|
| Linear | O(1) | O(n) | O(n) | No |
| Binary | O(1) | O(log n) | O(log n) | Yes |
| Ternary | O(1) | O(log n) | O(log n) | Yes |
| Jump | O(1) | O(√n) | O(√n) | Yes |
| Exponential | O(1) | O(log n) | O(log n) | Yes |
| Interpolation | O(1) | O(log log n)* | O(n) | Yes |
Scroll the table sideways for the rest of the columns.
Interpolation search only reaches that average when the gaps between the values are even.