Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,49 @@
|
|||
require "table2"
|
||||
|
||||
-- Counting sort of 'a' according to the digit represented by 'exp'.
|
||||
local function count_sort(a, exp)
|
||||
local n = #a
|
||||
local output = table.rep(n, 0)
|
||||
local count = table.rep(10, 0)
|
||||
for i = 1, n do
|
||||
local t = math.trunc(a[i] / exp) % 10
|
||||
count[t + 1] += 1
|
||||
end
|
||||
for i = 1, 9 do count[i + 1] += count[i] end
|
||||
for i = n, 1, -1 do
|
||||
local t = math.trunc(a[i] / exp) % 10
|
||||
output[count[t + 1]] = a[i]
|
||||
count[t + 1] -= 1
|
||||
end
|
||||
for i = 1, n do a[i] = output[i] end
|
||||
end
|
||||
|
||||
-- Sorts 'a' in place.
|
||||
local function radix_sort(a)
|
||||
-- Check for negative elements.
|
||||
local min = a:min()
|
||||
-- If there are any, increase all elements by -min.
|
||||
if min < 0 then
|
||||
for i = 1, #a do a[i] -= min end
|
||||
end
|
||||
-- Now get the maximum to know number of digits
|
||||
local max = a:max()
|
||||
-- Do counting sort for each digit
|
||||
local exp = 1
|
||||
while math.trunc(max / exp) > 0 do
|
||||
count_sort(a, exp)
|
||||
exp *= 10
|
||||
end
|
||||
-- If there were negative elements, reduce all elements by -min.
|
||||
if min < 0 then
|
||||
for i = 1, #a do a[i] += min end
|
||||
end
|
||||
end
|
||||
|
||||
local aa = {{4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {170, 45, 75, 90, 2, 24, -802, -66}}
|
||||
for aa as a do
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
radix_sort(a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue