Skip to content

Commit 585cf1d

Browse files
committed
Fix formatting
1 parent b6c98e9 commit 585cf1d

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

.spec/sorting/sort_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ describe("Bogosort", function()
7676
check_sort(require("sorting.bogosort"), 5)
7777
end)
7878
describe("Countingsort", function()
79-
check_sort(require("sorting.countingsort"), nil, true)
79+
check_sort(require("sorting.countingsort"), nil, true)
8080
end)

src/sorting/countingsort.lua

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
return function(
2-
-- list to be sorted in-place
3-
list,
4-
-- key_function to map elements to integer keys, defaults to identity
5-
key_function
2+
-- list to be sorted in-place
3+
list,
4+
-- key_function to map elements to integer keys, defaults to identity
5+
key_function
66
)
7-
if list[1] == nil then return end
8-
9-
key_function = key_function or function(x) return x end
10-
11-
-- Find the range of keys (min_key and max_key)
12-
local min_key, max_key = math.huge, -math.huge
13-
for _, elem in ipairs(list) do
14-
local key = key_function(elem)
15-
min_key = math.min(min_key, key)
16-
max_key = math.max(max_key, key)
17-
end
18-
19-
local count = {}
20-
for i = 1, max_key - min_key + 1 do
21-
count[i] = 0
22-
end
23-
24-
-- Count the occurrences of each key
25-
for _, elem in ipairs(list) do
26-
local key = key_function(elem)
27-
count[key - min_key + 1] = count[key - min_key + 1] + 1
28-
end
29-
30-
-- Compute cumulative counts for final positions
31-
for i = 2, #count do
32-
count[i] = count[i] + count[i - 1]
33-
end
34-
35-
local output = {}
36-
for i = #list, 1, -1 do
37-
local element = list[i]
38-
local key = key_function(element)
39-
output[count[key - min_key + 1]] = element
40-
count[key - min_key + 1] = count[key - min_key + 1] - 1
41-
end
42-
43-
for i, elem in ipairs(output) do
44-
list[i] = elem
45-
end
7+
if list[1] == nil then
8+
return
9+
end
10+
11+
key_function = key_function or function(x)
12+
return x
13+
end
14+
15+
-- Find the range of keys (min_key and max_key)
16+
local min_key, max_key = math.huge, -math.huge
17+
for _, elem in ipairs(list) do
18+
local key = key_function(elem)
19+
min_key = math.min(min_key, key)
20+
max_key = math.max(max_key, key)
21+
end
22+
23+
local count = {}
24+
for i = 1, max_key - min_key + 1 do
25+
count[i] = 0
26+
end
27+
28+
-- Count the occurrences of each key
29+
for _, elem in ipairs(list) do
30+
local key = key_function(elem)
31+
count[key - min_key + 1] = count[key - min_key + 1] + 1
32+
end
33+
34+
-- Compute cumulative counts for final positions
35+
for i = 2, #count do
36+
count[i] = count[i] + count[i - 1]
37+
end
38+
39+
local output = {}
40+
for i = #list, 1, -1 do
41+
local element = list[i]
42+
local key = key_function(element)
43+
output[count[key - min_key + 1]] = element
44+
count[key - min_key + 1] = count[key - min_key + 1] - 1
45+
end
46+
47+
for i, elem in ipairs(output) do
48+
list[i] = elem
49+
end
4650
end
47-

0 commit comments

Comments
 (0)