Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 01_introduction_to_algorithms/julia/binary_search.jil
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Test

function binary_search(arr,item)
low = 0
high = length(arr)

while low <= high
mid = (low + high) ÷ 2
guess = 0
try
guess = arr[mid]
catch
return nothing
end
if guess == item
return mid
elseif guess > item
high = mid - 1
else
low = mid + 1
end
end
end

arr = [1,3,5,7,9]
@test binary_search(arr,3) == 2
@test binary_search(arr,-1) == nothing