Created
May 20, 2018 05:52
-
-
Save sulmanweb/cd0ed0ede2cb883396aacc79d888af7b to your computer and use it in GitHub Desktop.
Find Fixed Point in Sorted Array Algorithm
This file contains 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
def find_fixed_point(array) | |
n = array.length | |
high = n - 1 | |
low = 0 | |
while low <= high | |
mid = low + (high - low / 2) | |
if array[mid] == mid | |
return mid | |
elsif array[mid] < mid | |
low = mid + 1 | |
else | |
high = mid - 1 | |
end | |
end | |
# if no fixed point exist | |
return -1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment