Created
October 12, 2012 03:39
-
-
Save flakyfilibuster/3877155 to your computer and use it in GitHub Desktop.
binary search
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
def binary_search(num, array) | |
return -1 if !array.include?(num) | |
index = (array.length)/2 | |
index_array = [(array.length), 0] | |
until num == array[index] | |
index = (index_array[0] + index_array[1])/2 | |
num > array[index] ? index_array[1] = index : index_array[0] = index | |
end | |
index | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment