Created
May 10, 2017 17:30
-
-
Save andrewsouthard1/6f0c198d7e87f746a37eb27ed6dbae29 to your computer and use it in GitHub Desktop.
Binary Search - Before Comments
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(n, arr) | |
middle = arr[arr.length / 2] | |
i = 0 | |
j = arr.length - 1 | |
while i < j | |
if middle == n | |
return true | |
elsif middle < n | |
i = middle | |
middle = i + j / 2 | |
else | |
j = middle | |
middle = i + j / 2 | |
end | |
end | |
false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment