-
-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Create cyclic_sort.py #9256
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
base: master
Are you sure you want to change the base?
Create cyclic_sort.py #9256
Conversation
for more information, see https://pre-commit.ci
|
The algorithm you propose basically only works if the list elements are within the range 1 to n. Naturally, |
Made the changes.
…On Sun, Oct 1, 2023 at 9:58 PM Tianyi Zheng ***@***.***> wrote:
1. The code is taken directly from the Stackademic article you
reference in #9251
<#9251>:
https://blog.stackademic.com/coding-pattern-cyclic-sort-96511b0f60ac
2. Like you said in #9251
<#9251>, one reason why
cyclic sort is a distinct algorithm from cycle sort is that
Cyclic sort assumes that the elements to be sorted are integers in a
specific range.
The algorithm you propose basically only works if the list elements are
within the range 1 to n. Naturally, cyclic_sort([-2, -5, -45]) would then
fail since you're indexing out of bounds of the array. Why add this as a
unit test when you're aware of this difference?
—
Reply to this email directly, view it on GitHub
<#9256 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ASOEZSDPGVMWECMWXYK7YYTX5GKZHANCNFSM6AAAAAA5OGWOBA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Hey can any of the admins review this, all tests have been passed. |
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 algorithm looks correct and nearly ready to be approved.
I'd just ask that you add input constraints and add those cases to your doctests.
|
||
def cyclic_sort(nums: list) -> list: | ||
""" | ||
Sorts the input list in-place using the Cyclic Sort algorithm. |
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.
I'd recommend adding the input constraints.
This algorithm only works correctly when the elements range from 1
to n
with no duplicates. Stating this constraint on the inputs to any potential users of the algorithm can help prevent confusion should they provide a list of integers that does not fit the criteria and end up in an infinite loop or some other unexpected behaviour.
|
||
Time complexity: O(n), where n is the number of elements in the list. | ||
|
||
Examples: |
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.
Similar to above, I'd recommend adding an example to demonstrate what happens if the list contains elements outside the expected range (a non-positive integer) or duplicates are present in the list. This would show why this algorithm should not be used for those sets of values.
>>> cyclic_sort([]) | ||
[] | ||
""" | ||
|
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 would be a good place to ensure the inputs meet the constraints of the algorithm. If they don't, indicate to the user what the issue is with the provided list.
""" | ||
|
||
|
||
def cyclic_sort(nums: list) -> list: |
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.
The algorithm is specified to sort integers, so a more correct typehint would be list[int]
for the nums
and the method output.
Describe your change:
Checklist: