|
1 |
| -function counting_sort(arr, key_function) |
2 |
| - -- If no key_function is provided, use identity function |
3 |
| - key_function = key_function or function(x) return x end |
4 |
| - |
5 |
| - -- Step 1: Find the range of keys (min_key and max_key) |
6 |
| - local min_key, max_key = math.huge, -math.huge |
7 |
| - for i = 1, #arr do |
8 |
| - local key = key_function(arr[i]) |
9 |
| - if key < min_key then |
10 |
| - min_key = key |
11 |
| - end |
12 |
| - if key > max_key then |
13 |
| - max_key = key |
14 |
| - end |
15 |
| - end |
16 |
| - |
17 |
| - -- Step 2: Initialize the count array |
18 |
| - local count = {} |
19 |
| - for i = min_key, max_key do |
20 |
| - count[i] = 0 |
21 |
| - end |
22 |
| - |
23 |
| - -- Step 3: Count the occurrences of each key. In this case key is same as arr[i] |
24 |
| - for i = 1, #arr do |
25 |
| - local key = key_function(arr[i]) |
26 |
| - count[key] = count[key] + 1 |
27 |
| - end |
28 |
| - |
29 |
| - -- Step 4: Compute cumulative counts to get final positions |
30 |
| - for i = min_key + 1, max_key do |
31 |
| - count[i] = count[i] + count[i - 1] |
32 |
| - end |
33 |
| - |
34 |
| - -- Step 5: Build the output array (in stable order) |
35 |
| - local output = {} |
36 |
| - for i = #arr, 1, -1 do |
37 |
| - local element = arr[i] |
38 |
| - local key = key_function(element) |
39 |
| - output[count[key]] = element |
40 |
| - count[key] = count[key] - 1 |
41 |
| - end |
42 |
| - |
43 |
| - -- Step 6: Copy the output array back to the original array |
44 |
| - for i = 1, #arr do |
45 |
| - arr[i] = output[i] |
46 |
| - end |
47 |
| - end |
48 |
| - |
49 |
| --- Sample array |
50 |
| -local arr = {100, 2, 2, 8, 3, 10000000, 1} |
51 |
| - |
52 |
| --- Simple usage with identity key function |
53 |
| -counting_sort(arr) |
54 |
| - |
55 |
| --- Print sorted array |
56 |
| -for i, v in ipairs(arr) do |
57 |
| - print(v) |
| 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 |
| 6 | +) |
| 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 |
| 11 | + if #list == 0 then return end |
| 12 | + |
| 13 | + -- Find the range of keys (min_key and max_key) |
| 14 | + local min_key, max_key = math.huge, -math.huge |
| 15 | + for _, elem in ipairs(list) do |
| 16 | + local key = key_function(elem) |
| 17 | + min_key = math.min(min_key, key) |
| 18 | + max_key = math.max(max_key, key) |
| 19 | + end |
| 20 | + |
| 21 | + -- Initialize the count array |
| 22 | + local count = {} |
| 23 | + for i = 1, (max_key - min_key + 1) do |
| 24 | + count[i] = 0 |
| 25 | + end |
| 26 | + |
| 27 | + -- Count then occurrences of each key |
| 28 | + for _, elem in ipairs(list) do |
| 29 | + local key = key_function(elem) |
| 30 | + count[key - min_key + 1] = count[key - min_key + 1] + 1 |
| 31 | + end |
| 32 | + |
| 33 | + -- Compute cumulative counts for final positions |
| 34 | + for i = 2, ipairs(count) do |
| 35 | + count[i] = count[i] + count[i - 1] |
| 36 | + end |
| 37 | + |
| 38 | + -- Build the output array (in stable order) |
| 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 | + -- Copy the output array back to the original list |
| 48 | + for i, elem in ipairs(output) do |
| 49 | + list[i] = elem |
| 50 | + end |
58 | 51 | end
|
| 52 | + |
0 commit comments