Created
May 13, 2021 10:03
-
-
Save cuongld2/8e4fed9ba44ea2b4598f90e7d5b6c612 to your computer and use it in GitHub Desktop.
Binary search in Julia
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
function binary_search(list,item) | |
low = 0 | |
high = size(list)[1] | |
while low <= high | |
mid = (low + high) | |
guess = list[mid] | |
if guess == item | |
return mid | |
end | |
if guess > item | |
high = mid - 1 | |
else | |
low = mid + 1 | |
return 0 | |
end | |
end | |
end | |
my_list = [1,3,5,6,8,10,12,21,34] | |
println(binary_search(my_list, 3)) | |
println(binary_search(my_list, 31)) | |
println(binary_search(my_list, 34)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment