Last active
April 26, 2019 04:54
-
-
Save kartikkukreja/55e87e5ec1600c5ab8da to your computer and use it in GitHub Desktop.
Interpolation Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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