Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,37 @@
|
|||
require "table2"
|
||||
|
||||
local function merge(left, right)
|
||||
local result = {}
|
||||
while #left > 0 and #right > 0 do
|
||||
if left[1] <= right[1] then
|
||||
result:insert(left[1])
|
||||
left = left:slice(2)
|
||||
else
|
||||
result:insert(right[1])
|
||||
right = right:slice(2)
|
||||
end
|
||||
end
|
||||
if #left > 0 then result:join(left) end
|
||||
if #right > 0 then result:join(right) end
|
||||
return result
|
||||
end
|
||||
|
||||
local function merge_sort(m)
|
||||
local len = #m
|
||||
if len <= 1 then return m end
|
||||
local middle = len // 2
|
||||
local left = m:slice(1, middle)
|
||||
local right = m:slice(middle + 1)
|
||||
left = merge_sort(left)
|
||||
right = merge_sort(right)
|
||||
if left:back() <= right[1] then return left:join(right) end
|
||||
return merge(left, right)
|
||||
end
|
||||
|
||||
local array = { {4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {7, 5, 2, 6, 1, 4, 2, 6, 3} }
|
||||
for array as a do
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
a = merge_sort(a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue