Skip to content

Instantly share code, notes, and snippets.

@AndrewGuard
Last active January 1, 2016 08:29
Show Gist options
  • Select an option

  • Save AndrewGuard/8118387 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewGuard/8118387 to your computer and use it in GitHub Desktop.
def binary_search(num, array, offset = 0)
mid = (array.length - 1)/2
if array[mid] == num
return mid + offset
end
if array[mid] < num
offset += mid + 1
binary_search(num, array[(mid + 1).. -1], offset)
elsif array[mid] > num
binary_search(num, array[0..(mid - 1)], offset)
end
end
array = (101..200).to_a
p binary_search(112, array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment