From b77dacd0ef85b0c0798c1a6274906a9c15572268 Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Sat, 5 Oct 2024 21:03:16 +0530 Subject: [PATCH 01/13] Create countingsort.lua --- src/sorting/countingsort.lua | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/sorting/countingsort.lua diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/sorting/countingsort.lua @@ -0,0 +1 @@ + From 593d66b2b9bb30e6082ae9bf27d297b2442415bf Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Sat, 5 Oct 2024 21:08:41 +0530 Subject: [PATCH 02/13] Update countingsort.lua --- src/sorting/countingsort.lua | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 8b13789..455e536 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -1 +1,58 @@ +function counting_sort(arr, key_function) + -- If no key_function is provided, use identity function + key_function = key_function or function(x) return x end + -- Step 1: Find the range of keys (min_key and max_key) + local min_key, max_key = math.huge, -math.huge + for i = 1, #arr do + local key = key_function(arr[i]) + if key < min_key then + min_key = key + end + if key > max_key then + max_key = key + end + end + + -- Step 2: Initialize the count array + local count = {} + for i = min_key, max_key do + count[i] = 0 + end + + -- Step 3: Count the occurrences of each key. In this case key is same as arr[i] + for i = 1, #arr do + local key = key_function(arr[i]) + count[key] = count[key] + 1 + end + + -- Step 4: Compute cumulative counts to get final positions + for i = min_key + 1, max_key do + count[i] = count[i] + count[i - 1] + end + + -- Step 5: Build the output array (in stable order) + local output = {} + for i = #arr, 1, -1 do + local element = arr[i] + local key = key_function(element) + output[count[key]] = element + count[key] = count[key] - 1 + end + + -- Step 6: Copy the output array back to the original array + for i = 1, #arr do + arr[i] = output[i] + end + end + +-- Sample array +local arr = {100, 2, 2, 8, 3, 10000000, 1} + +-- Simple usage with identity key function +counting_sort(arr) + +-- Print sorted array +for i, v in ipairs(arr) do + print(v) +end From 2be6f6bda61b3e3d56a1505275efd1b6aacc25d8 Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Mon, 7 Oct 2024 19:50:59 +0530 Subject: [PATCH 03/13] Update countingsort.lua --- src/sorting/countingsort.lua | 108 +++++++++++++++++------------------ 1 file changed, 51 insertions(+), 57 deletions(-) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 455e536..07049f2 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -1,58 +1,52 @@ -function counting_sort(arr, key_function) - -- If no key_function is provided, use identity function - key_function = key_function or function(x) return x end - - -- Step 1: Find the range of keys (min_key and max_key) - local min_key, max_key = math.huge, -math.huge - for i = 1, #arr do - local key = key_function(arr[i]) - if key < min_key then - min_key = key - end - if key > max_key then - max_key = key - end - end - - -- Step 2: Initialize the count array - local count = {} - for i = min_key, max_key do - count[i] = 0 - end - - -- Step 3: Count the occurrences of each key. In this case key is same as arr[i] - for i = 1, #arr do - local key = key_function(arr[i]) - count[key] = count[key] + 1 - end - - -- Step 4: Compute cumulative counts to get final positions - for i = min_key + 1, max_key do - count[i] = count[i] + count[i - 1] - end - - -- Step 5: Build the output array (in stable order) - local output = {} - for i = #arr, 1, -1 do - local element = arr[i] - local key = key_function(element) - output[count[key]] = element - count[key] = count[key] - 1 - end - - -- Step 6: Copy the output array back to the original array - for i = 1, #arr do - arr[i] = output[i] - end - end - --- Sample array -local arr = {100, 2, 2, 8, 3, 10000000, 1} - --- Simple usage with identity key function -counting_sort(arr) - --- Print sorted array -for i, v in ipairs(arr) do - print(v) +return function( + -- list to be sorted in-place + list, + -- key_function to map elements to integer keys, defaults to identity + key_function +) + -- Default to identity function if no key_function is provided + key_function = key_function or function(x) return x end + + -- Handle empty list case + if #list == 0 then return end + + -- Find the range of keys (min_key and max_key) + local min_key, max_key = math.huge, -math.huge + for _, elem in ipairs(list) do + local key = key_function(elem) + min_key = math.min(min_key, key) + max_key = math.max(max_key, key) + end + + -- Initialize the count array + local count = {} + for i = 1, (max_key - min_key + 1) do + count[i] = 0 + end + + -- Count then occurrences of each key + for _, elem in ipairs(list) do + local key = key_function(elem) + count[key - min_key + 1] = count[key - min_key + 1] + 1 + end + + -- Compute cumulative counts for final positions + for i = 2, ipairs(count) do + count[i] = count[i] + count[i - 1] + end + + -- Build the output array (in stable order) + local output = {} + for i = #list, 1, -1 do + local element = list[i] + local key = key_function(element) + output[count[key - min_key + 1]] = element + count[key - min_key + 1] = count[key - min_key + 1] - 1 + end + + -- Copy the output array back to the original list + for i, elem in ipairs(output) do + list[i] = elem + end end + From c08bc1a5f3a841c1fa1b6e450005b3781b852ca8 Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Mon, 7 Oct 2024 19:52:59 +0530 Subject: [PATCH 04/13] Update sort_spec.lua --- .spec/sorting/sort_spec.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index 5f2132f..5a470ac 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -75,3 +75,10 @@ end) describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) +describe("Countingsort", function() + -- Test with multiple radii + local countingsort = require("sorting.countingsort") + check_sort(countingsort(), nil, true) + check_sort(countingsort(2), nil, true) + check_sort(countingsort(1e3), nil, true) +end) From a3c6be4d2e41a86fde763fc1c4376d4c5bd3dcac Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:03:58 +0530 Subject: [PATCH 05/13] Update src/sorting/countingsort.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --- src/sorting/countingsort.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 07049f2..7bafa86 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -8,7 +8,7 @@ return function( key_function = key_function or function(x) return x end -- Handle empty list case - if #list == 0 then return end + if list[1] == nil then return end -- Find the range of keys (min_key and max_key) local min_key, max_key = math.huge, -math.huge From fbd5c22f18764c2cf757d996a6d271e71247b0fe Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:11:55 +0530 Subject: [PATCH 06/13] Update countingsort.lua --- src/sorting/countingsort.lua | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 7bafa86..f31006c 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -4,12 +4,10 @@ return function( -- key_function to map elements to integer keys, defaults to identity key_function ) - -- Default to identity function if no key_function is provided - key_function = key_function or function(x) return x end - - -- Handle empty list case if list[1] == nil then return end + key_function = key_function or function(x) return x end + -- Find the range of keys (min_key and max_key) local min_key, max_key = math.huge, -math.huge for _, elem in ipairs(list) do @@ -18,7 +16,6 @@ return function( max_key = math.max(max_key, key) end - -- Initialize the count array local count = {} for i = 1, (max_key - min_key + 1) do count[i] = 0 @@ -35,7 +32,6 @@ return function( count[i] = count[i] + count[i - 1] end - -- Build the output array (in stable order) local output = {} for i = #list, 1, -1 do local element = list[i] @@ -44,7 +40,6 @@ return function( count[key - min_key + 1] = count[key - min_key + 1] - 1 end - -- Copy the output array back to the original list for i, elem in ipairs(output) do list[i] = elem end From 9f471aeb75f8c819187e2a488664d3e03e836d79 Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:13:48 +0530 Subject: [PATCH 07/13] Update src/sorting/countingsort.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --- src/sorting/countingsort.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index f31006c..200368a 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -17,7 +17,7 @@ return function( end local count = {} - for i = 1, (max_key - min_key + 1) do + for i = 1, max_key - min_key + 1 do count[i] = 0 end From 39e5284ee336aa4c17ad4780472e0b4bb13ca0aa Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:49:22 +0530 Subject: [PATCH 08/13] Update sort_spec.lua --- .spec/sorting/sort_spec.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index 5a470ac..0662070 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -76,9 +76,6 @@ describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) describe("Countingsort", function() - -- Test with multiple radii local countingsort = require("sorting.countingsort") - check_sort(countingsort(), nil, true) - check_sort(countingsort(2), nil, true) - check_sort(countingsort(1e3), nil, true) + check_sort(require(countingsort()), nil, true) end) From 495b01c93c5386f483426a8fb6444f8205650c25 Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:50:54 +0530 Subject: [PATCH 09/13] Update sort_spec.lua --- .spec/sorting/sort_spec.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index 0662070..cef1c94 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -76,6 +76,5 @@ describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) describe("Countingsort", function() - local countingsort = require("sorting.countingsort") - check_sort(require(countingsort()), nil, true) + check_sort(require("sorting.countingsort"), nil, true) end) From dbf28e9bc88c67ad23df19e4e4b1343d9915d8eb Mon Sep 17 00:00:00 2001 From: icemberg <135527820+icemberg@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:54:41 +0530 Subject: [PATCH 10/13] Update sort_spec.lua --- .spec/sorting/sort_spec.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index cef1c94..6e7fb1c 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -76,5 +76,6 @@ describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) describe("Countingsort", function() - check_sort(require("sorting.countingsort"), nil, true) + local countingsort = require("sorting.countingsort") + check_sort(countingsort, nil, true) end) From b73ddb6d9b751c8725359884e157526187769d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:52:56 +0200 Subject: [PATCH 11/13] Update countingsort.lua --- src/sorting/countingsort.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 200368a..36f31f8 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -21,14 +21,14 @@ return function( count[i] = 0 end - -- Count then occurrences of each key + -- Count the occurrences of each key for _, elem in ipairs(list) do local key = key_function(elem) count[key - min_key + 1] = count[key - min_key + 1] + 1 end -- Compute cumulative counts for final positions - for i = 2, ipairs(count) do + for i = 2, #count do count[i] = count[i] + count[i - 1] end From b6c98e9ffdc06a285b0457439ffd6f9fd82cb0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:53:13 +0200 Subject: [PATCH 12/13] Update sort_spec.lua --- .spec/sorting/sort_spec.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index 6e7fb1c..783f408 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -76,6 +76,5 @@ describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) describe("Countingsort", function() - local countingsort = require("sorting.countingsort") - check_sort(countingsort, nil, true) + check_sort(require("sorting.countingsort"), nil, true) end) From 585cf1dfbbcf4ee074327de5a16f01c5bf3238af Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Thu, 10 Oct 2024 14:56:50 +0200 Subject: [PATCH 13/13] Fix formatting --- .spec/sorting/sort_spec.lua | 2 +- src/sorting/countingsort.lua | 91 +++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/.spec/sorting/sort_spec.lua b/.spec/sorting/sort_spec.lua index 783f408..cef1c94 100644 --- a/.spec/sorting/sort_spec.lua +++ b/.spec/sorting/sort_spec.lua @@ -76,5 +76,5 @@ describe("Bogosort", function() check_sort(require("sorting.bogosort"), 5) end) describe("Countingsort", function() - check_sort(require("sorting.countingsort"), nil, true) + check_sort(require("sorting.countingsort"), nil, true) end) diff --git a/src/sorting/countingsort.lua b/src/sorting/countingsort.lua index 36f31f8..6b032dd 100644 --- a/src/sorting/countingsort.lua +++ b/src/sorting/countingsort.lua @@ -1,47 +1,50 @@ return function( - -- list to be sorted in-place - list, - -- key_function to map elements to integer keys, defaults to identity - key_function + -- list to be sorted in-place + list, + -- key_function to map elements to integer keys, defaults to identity + key_function ) - if list[1] == nil then return end - - key_function = key_function or function(x) return x end - - -- Find the range of keys (min_key and max_key) - local min_key, max_key = math.huge, -math.huge - for _, elem in ipairs(list) do - local key = key_function(elem) - min_key = math.min(min_key, key) - max_key = math.max(max_key, key) - end - - local count = {} - for i = 1, max_key - min_key + 1 do - count[i] = 0 - end - - -- Count the occurrences of each key - for _, elem in ipairs(list) do - local key = key_function(elem) - count[key - min_key + 1] = count[key - min_key + 1] + 1 - end - - -- Compute cumulative counts for final positions - for i = 2, #count do - count[i] = count[i] + count[i - 1] - end - - local output = {} - for i = #list, 1, -1 do - local element = list[i] - local key = key_function(element) - output[count[key - min_key + 1]] = element - count[key - min_key + 1] = count[key - min_key + 1] - 1 - end - - for i, elem in ipairs(output) do - list[i] = elem - end + if list[1] == nil then + return + end + + key_function = key_function or function(x) + return x + end + + -- Find the range of keys (min_key and max_key) + local min_key, max_key = math.huge, -math.huge + for _, elem in ipairs(list) do + local key = key_function(elem) + min_key = math.min(min_key, key) + max_key = math.max(max_key, key) + end + + local count = {} + for i = 1, max_key - min_key + 1 do + count[i] = 0 + end + + -- Count the occurrences of each key + for _, elem in ipairs(list) do + local key = key_function(elem) + count[key - min_key + 1] = count[key - min_key + 1] + 1 + end + + -- Compute cumulative counts for final positions + for i = 2, #count do + count[i] = count[i] + count[i - 1] + end + + local output = {} + for i = #list, 1, -1 do + local element = list[i] + local key = key_function(element) + output[count[key - min_key + 1]] = element + count[key - min_key + 1] = count[key - min_key + 1] - 1 + end + + for i, elem in ipairs(output) do + list[i] = elem + end end -