Create cyclic_sort.py
Describe your change:
- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?
Checklist:
- [x] I have read CONTRIBUTING.md.
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Python files are placed inside an existing directory.
- [x] All filenames are in all lowercase characters with no spaces or dashes.
- [x] All functions and variable names follow Python naming conventions.
- [x] All function parameters and return values are annotated with Python type hints.
- [x] All functions have doctests that pass the automated testing.
- [x] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
- [x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".
027 >>> cyclic_sort([-2, -5, -45])
UNEXPECTED EXCEPTION: IndexError('list index out of range')
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.11.5/x64/lib/python3.11/doctest.py", line 1351, in __run
exec(compile(example.source, filename, "single",
File "<doctest sorts.cyclic_sort.cyclic_sort[2]>", line 1, in <module>
File "/home/runner/work/Python/Python/sorts/cyclic_sort.py", line 39, in cyclic_sort
if nums[i] != nums[correct_index]:
~~~~^^^^^^^^^^^^^^^
IndexError: list index out of range
/home/runner/work/Python/Python/sorts/cyclic_sort.py:27: UnexpectedException
- The code is taken directly from the Stackademic article you reference in #9251: https://blog.stackademic.com/coding-pattern-cyclic-sort-96511b0f60ac
- Like you said in #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?
Made the changes.
On Sun, Oct 1, 2023 at 9:58 PM Tianyi Zheng @.***> wrote:
- The code is taken directly from the Stackademic article you reference in #9251 https://github.com/TheAlgorithms/Python/issues/9251: https://blog.stackademic.com/coding-pattern-cyclic-sort-96511b0f60ac
- Like you said in #9251 https://github.com/TheAlgorithms/Python/issues/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 https://github.com/TheAlgorithms/Python/pull/9256#issuecomment-1742131145, 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.