Skip to content

Commit cf01c72

Browse files
authored
Create binarysearch.lua
1 parent db41024 commit cf01c72

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

DataStructures/Trees/binarysearch.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
binarysearch = {}
2+
binarysearch.__index = binarysearch
3+
4+
function binarysearch.indexOf(a, value, comparator)
5+
local lo = 0
6+
local hi = a:size() - 1
7+
while lo <= hi do
8+
local mid = lo + math.floor((hi - lo) / 2)
9+
local cmp = comparator(value, a:get(mid))
10+
if cmp < 0 then
11+
hi = mid - 1
12+
elseif cmp > 0 then
13+
lo = mid + 1
14+
else
15+
return mid
16+
end
17+
end
18+
return -1
19+
20+
end
21+
22+
return binarysearch

0 commit comments

Comments
 (0)