Created
February 12, 2020 07:20
-
-
Save arpit-omprakash/46f6bdd9682836d16325594a694d17c3 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 binarysearch(lst::Vector{T}, value::T, low=1, high=length(lst)) where T | |
if isempty(lst) return 0 end | |
if low ≥ high | |
if low > high || lst[low] != value | |
return 0 | |
else | |
return low | |
end | |
end | |
mid = (low + high) ÷ 2 | |
if lst[mid] > value | |
return binarysearch(lst, value, low, mid-1) | |
elseif lst[mid] < value | |
return binarysearch(lst, value, mid+1, high) | |
else | |
return mid | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment