Last active
August 29, 2015 14:11
-
-
Save wendy0402/0df7a0233e5767de9739 to your computer and use it in GitHub Desktop.
alternative simple implementation of tail recursion for binary search algorithm
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
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