|
1 | 1 | 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 |
6 | 6 | )
|
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 |
46 | 50 | end
|
47 |
| - |
|
0 commit comments