Created
April 22, 2013 22:39
-
-
Save moserrya/5439201 to your computer and use it in GitHub Desktop.
Find the smallest number in a shifted sorted array that runs in log(n) time
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_index(ary, index = 0) | |
midpt = ary.length / 2 | |
if midpt <= 1 | |
return ary.first < ary.last ? index : index + 1 | |
end | |
left, right = ary[0...midpt], ary[midpt..-1] | |
if left.last > left.first && ary.last < ary.first | |
index += midpt | |
find_index(right, index) | |
else | |
find_index(left, index) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment