Skip to content

Instantly share code, notes, and snippets.

@wendy0402
Last active August 29, 2015 14:11
Show Gist options
  • Save wendy0402/0df7a0233e5767de9739 to your computer and use it in GitHub Desktop.
Save wendy0402/0df7a0233e5767de9739 to your computer and use it in GitHub Desktop.
alternative simple implementation of tail recursion for binary search algorithm
define_method(:search) do |value, array, from = 0, to = (array.size - 1)|
mid = (from + to) / 2
return mid if value == array[mid]
return nil if to == 0 || from == (array.size - 1)
value < array[mid] ? to = mid - 1 : from = mid + 1
redo
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment