Created
October 10, 2012 00:20
-
-
Save rvbsanjose/3862365 to your computer and use it in GitHub Desktop.
Binary search
This file contains hidden or 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
def binary_search(number, random_numbers) | |
middle = random_numbers.length / 2 | |
until random_numbers[middle] == number | |
if random_numbers[middle] < number | |
middle += random_numbers[middle..-1].length / 2 | |
end | |
if random_numbers[middle] > number | |
middle -= random_numbers[0..middle].length / 2 | |
end | |
end | |
middle | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment