Created
October 10, 2012 02:55
-
-
Save scottchiang/3862887 to your computer and use it in GitHub Desktop.
Solution for Binary Search
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 binary_search(obj, array, lower = 0, upper = array.length) | |
mid_point = ((upper - lower) / 2) + lower | |
if obj != array[mid_point] && (mid_point == array.length - 1 || mid_point == 0) | |
return -1 | |
elsif obj == array[mid_point] | |
return mid_point | |
elsif obj < array[mid_point] | |
binary_search(obj, array, lower, mid_point) | |
elsif obj > array[mid_point] | |
binary_search(obj, array, mid_point, upper) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment