-
-
Notifications
You must be signed in to change notification settings - Fork 63
Added countingsort.lua #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, Step X: ...
comments should just be ...
. The enumeration isn't helpful and is harmful when "steps" are added / removed.
src/sorting/countingsort.lua
Outdated
@@ -0,0 +1,58 @@ | |||
function counting_sort(arr, key_function) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This function should be
return
ed directly forrequire
to work with this as expected rather than polluting globals; - I would rename
arr
tolist
for consistency with the rest of the code. key_function
should have a comment explaining what it does and what it defaults to. Especially since this sorting algorithm has special requirements (keys must be integers).
src/sorting/countingsort.lua
Outdated
-- 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is self-explanatory, no comment needed. You can also simplify this to
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
src/sorting/countingsort.lua
Outdated
|
||
-- Step 2: Initialize the count array | ||
local count = {} | ||
for i = min_key, max_key do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You get a problem here when arr
is empty. Please add a special case to return early in that case.
Furthermore, as it currently is, this may (unnecessarily) populate the hash part if min_key
is not 1
, so please shift accordingly to prevent that.
src/sorting/countingsort.lua
Outdated
end | ||
|
||
-- Step 3: Count the occurrences of each key. In this case key is same as arr[i] | ||
for i = 1, #arr do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again use ipairs
src/sorting/countingsort.lua
Outdated
count[i] = 0 | ||
end | ||
|
||
-- Step 3: Count the occurrences of each key. In this case key is same as arr[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary comment, also slightly misleading (keys are produced by the key function).
src/sorting/countingsort.lua
Outdated
end | ||
end | ||
|
||
-- Sample array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add proper tests rather than some example code at the end of the file. This can essentially use the same tests as Radix Sort: check_sort(countingsort, nil, true)
, see https://github.com/TheAlgorithms/Lua/blob/main/.spec/sorting/sort_spec.lua.
Taken all the specified changes into account.. |
Made all the changes |
Can you please check this once.. |
src/sorting/countingsort.lua
Outdated
max_key = math.max(max_key, key) | ||
end | ||
|
||
-- Initialize the count array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- Initialize the count array |
src/sorting/countingsort.lua
Outdated
-- key_function to map elements to integer keys, defaults to identity | ||
key_function | ||
) | ||
-- Default to identity function if no key_function is provided |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- Default to identity function if no key_function is provided |
src/sorting/countingsort.lua
Outdated
-- Default to identity function if no key_function is provided | ||
key_function = key_function or function(x) return x end | ||
|
||
-- Handle empty list case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- Handle empty list case |
(If anything a useful comment would explain why not handling it early creates problems further down the line. The fact that we are handling it here early is obvious.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I am sorry for this issue.....
Co-authored-by: Lars Müller <[email protected]>
Co-authored-by: Lars Müller <[email protected]>
Removed the comments that you mentioned . Retained the comments which have the core logic for this sorting technique. |
.spec/sorting/sort_spec.lua
Outdated
end) | ||
describe("Countingsort", function() | ||
-- Test with multiple radii | ||
local countingsort = require("sorting.countingsort") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local countingsort = require("sorting.countingsort") |
Is it correct now? |
Please mention the label for hacktoberfest and gssoc' extd 24 . It will be helpful. I am willing to contribute further . Can you please help me if I have any doubts??