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 Interpolation Search estimates the position using linear interpolation, ideal for uniformly distributed data.
Click Start to begin Interpolation Search visualization
This formula estimates where the target should be based on its value relative to the range.
Works best when data is uniformly distributed.
def interpolation_search(arr, target):
left, right = 0, len(arr) - 1
while (left <= right and target >= arr[left] and target <= arr[right]):
# If we have a single element
if left == right:
if arr[left] == target:
return left
else:
return -1
# Interpolation formula
pos = left + int(((target - arr[left]) * (right - left)) / (arr[right] - arr[left]))
if arr[pos] == target:
return pos
elif arr[pos] < target:
left = pos + 1
else:
right = pos - 1
return -1