Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sharansukesh1003
Copy link

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • 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".

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 1, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 1, 2023
@cclauss
Copy link
Member

cclauss commented Oct 1, 2023

 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

@tianyizheng02
Copy link
Contributor

  1. The code is taken directly from the Stackademic article you reference in Feature: Cyclic Sort #9251: https://blog.stackademic.com/coding-pattern-cyclic-sort-96511b0f60ac
  2. Like you said in Feature: Cyclic Sort #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?

@sharansukesh1003
Copy link
Author

sharansukesh1003 commented Oct 1, 2023 via email

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 1, 2023
@sharansukesh1003
Copy link
Author

Hey can any of the admins review this, all tests have been passed.

Copy link
Contributor

@jeffreyyancey jeffreyyancey left a 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.
Copy link
Contributor

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:
Copy link
Contributor

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([])
[]
"""

Copy link
Contributor

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:
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants