Skip to content

Instantly share code, notes, and snippets.

@arpit-omprakash
Created February 12, 2020 07:20
Show Gist options
  • Save arpit-omprakash/46f6bdd9682836d16325594a694d17c3 to your computer and use it in GitHub Desktop.
Save arpit-omprakash/46f6bdd9682836d16325594a694d17c3 to your computer and use it in GitHub Desktop.
Binary search in Julia
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