Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,46 @@
do
local function lower_bound(container, container_begin, container_end, value, comparator)
local count = container_end - container_begin + 1
while count > 0 do
local half = bit.rshift(count, 1) -- or math.floor(count / 2)
local middle = container_begin + half
if comparator(container[middle], value) then
container_begin = middle + 1
count = count - half - 1
else
count = half
end
end
return container_begin
end
local function binary_insertion_sort_impl(container, comparator)
for i = 2, #container do
local j = i - 1
local selected = container[i]
local loc = lower_bound(container, 1, j, selected, comparator)
while j >= loc do
container[j + 1] = container[j]
j = j - 1
end
container[j + 1] = selected
end
end
local function binary_insertion_sort_comparator(a, b)
return a < b
end
function table.bininsertionsort(container, comparator)
if not comparator then
comparator = binary_insertion_sort_comparator
end
binary_insertion_sort_impl(container, comparator)
end
end

View file

@ -0,0 +1,16 @@
function bins(tb, val, st, en)
local st, en = st or 1, en or #tb
local mid = math.floor((st + en)/2)
if en == st then return tb[st] > val and st or st+1
else return tb[mid] > val and bins(tb, val, st, mid) or bins(tb, val, mid+1, en)
end
end
function isort(t)
local ret = {t[1], t[2]}
for i = 3, #t do
table.insert(ret, bins(ret, t[i]), t[i])
end
return ret
end
print(unpack(isort{4,5,2,7,8,3}))