Skip to content

Instantly share code, notes, and snippets.

@flakyfilibuster
Created October 12, 2012 03:39
Show Gist options
  • Save flakyfilibuster/3877155 to your computer and use it in GitHub Desktop.
Save flakyfilibuster/3877155 to your computer and use it in GitHub Desktop.
binary search
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