How would I write a binary search method if the value I'm looking for might not be in the array?
def binary_search(array, value, from=0, to=nil)
to = array.count - 1 unless to
mid = (from + to) / 2
if value < array[mid]
return binary_search(array, value, from, mid - 1)
elsif value > array[mid]
return binary_search(array, value, mid + 1, to)
stack level too deep (SystemStackError
Is this homework? If not, then just use Array#bsearch.
If it is homework, then consider what happens when you reach the point that you have no more values to test; your
to will be <= your
from (that is, the size of your search space is empty - there are no more values left to test). In the case that happens, you should raise an exception or return some value which is interpreted as "value not found".