Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Binary-search/Julia/binary-search-1.julia
Normal file
15
Task/Binary-search/Julia/binary-search-1.julia
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
function binarysearch(lst::Vector{T}, val::T) where T
|
||||
low = 1
|
||||
high = length(lst)
|
||||
while low ≤ high
|
||||
mid = (low + high) ÷ 2
|
||||
if lst[mid] > val
|
||||
high = mid - 1
|
||||
elseif lst[mid] < val
|
||||
low = mid + 1
|
||||
else
|
||||
return mid
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
18
Task/Binary-search/Julia/binary-search-2.julia
Normal file
18
Task/Binary-search/Julia/binary-search-2.julia
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue