RosettaCodeData/Task/Binary-search/Lua/binary-search-1.lua

13 lines
312 B
Lua
Raw Permalink Normal View History

2013-04-10 12:38:42 -07:00
function binarySearch (list,value)
local low = 1
local high = #list
while low <= high do
2019-09-12 10:33:56 -07:00
local mid = math.floor((low+high)/2)
2013-04-10 12:38:42 -07:00
if list[mid] > value then high = mid - 1
2019-09-12 10:33:56 -07:00
elseif list[mid] < value then low = mid + 1
else return mid
2013-04-10 12:38:42 -07:00
end
end
return false
end