Skip to content

Commit fbd5c22

Browse files
authored
Update countingsort.lua
1 parent a3c6be4 commit fbd5c22

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/sorting/countingsort.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ return function(
44
-- key_function to map elements to integer keys, defaults to identity
55
key_function
66
)
7-
-- Default to identity function if no key_function is provided
8-
key_function = key_function or function(x) return x end
9-
10-
-- Handle empty list case
117
if list[1] == nil then return end
128

9+
key_function = key_function or function(x) return x end
10+
1311
-- Find the range of keys (min_key and max_key)
1412
local min_key, max_key = math.huge, -math.huge
1513
for _, elem in ipairs(list) do
@@ -18,7 +16,6 @@ return function(
1816
max_key = math.max(max_key, key)
1917
end
2018

21-
-- Initialize the count array
2219
local count = {}
2320
for i = 1, (max_key - min_key + 1) do
2421
count[i] = 0
@@ -35,7 +32,6 @@ return function(
3532
count[i] = count[i] + count[i - 1]
3633
end
3734

38-
-- Build the output array (in stable order)
3935
local output = {}
4036
for i = #list, 1, -1 do
4137
local element = list[i]
@@ -44,7 +40,6 @@ return function(
4440
count[key - min_key + 1] = count[key - min_key + 1] - 1
4541
end
4642

47-
-- Copy the output array back to the original list
4843
for i, elem in ipairs(output) do
4944
list[i] = elem
5045
end

0 commit comments

Comments
 (0)