June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,15 +1,15 @@
function binary_search(l, value)
function binarysearch(lst::Vector{T}, val::T) where T
low = 1
high = length(l)
while low <= high
mid = int((low+high)/2)
if l[mid] > value
high = mid-1
elseif l[mid] < value
low = mid+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
return mid
end
end
return -1
return 0
end

View file

@ -1,10 +1,18 @@
function binary_search(l, value, low = 1, high = -1)
high == -1 && (high = length(l))
l==[] && (return -1)
low >= high &&
((low > high || l[low] != value) ? (return -1) : return low)
mid = int((low+high)/2)
l[mid] > value ? (return binary_search(l, value, low, mid-1)) :
l[mid] < value ? (return binary_search(l, value, mid+1, high)) :
return mid
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