Last active
June 28, 2022 20:33
-
-
Save andrewsouthard1/a3dacbddfe3f71742a2ef52874c93024 to your computer and use it in GitHub Desktop.
Binary Search - Complete
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.length / 2 | |
i = 0 | |
j = arr.length - 1 | |
while i < j | |
if arr[middle] == n | |
return true | |
elsif arr[middle] < n | |
i = middle + 1 | |
middle = i + j / 2 | |
else | |
j = middle - 1 | |
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
This is incorrect as well.
This is a working solution + simple tests