Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created May 13, 2021 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/8e4fed9ba44ea2b4598f90e7d5b6c612 to your computer and use it in GitHub Desktop.
Save cuongld2/8e4fed9ba44ea2b4598f90e7d5b6c612 to your computer and use it in GitHub Desktop.
Binary search in Julia
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