DSAverse
Initializing Sorting Algorithms...
Sorting Algorithm
Binary Tree
Graph Traversal
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Watch how Exponential Search finds the range exponentially, then performs binary search within that range.
Click Start to begin Exponential Search visualization
def exponential_search(arr, target):
# If target is at first position
if arr[0] == target:
return 0
# Find range for binary search
bound = 1
while bound < len(arr) and arr[bound] < target:
bound *= 2
# Binary search in found range
left = bound // 2
right = min(bound, len(arr) - 1)
return binary_search(arr, target, left, right)
def binary_search(arr, target, left, right):
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1