Skip to content

Instantly share code, notes, and snippets.

@scottchiang
Created October 10, 2012 02:55
Show Gist options
  • Save scottchiang/3862887 to your computer and use it in GitHub Desktop.
Save scottchiang/3862887 to your computer and use it in GitHub Desktop.
Solution for Binary Search
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