Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active April 26, 2019 04:54
Show Gist options
  • Save kartikkukreja/55e87e5ec1600c5ab8da to your computer and use it in GitHub Desktop.
Save kartikkukreja/55e87e5ec1600c5ab8da to your computer and use it in GitHub Desktop.
Interpolation Search
interpolation_search(A[0...n-1], x):
i = 0, j = n-1, lo = A[i], hi = A[j]
if x < lo:
return -1
if x >= hi:
i = j
while i < j:
k = i + ((j - i) * (x - lo)) / (hi - lo)
mid = A[k]
if x > mid:
i = k + 1
lo = mid
elif x < mid:
j = k - 1
hi = mid
else:
return k
return (x != A[i]) ? -1 : i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment