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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions sorts/cyclic_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
This is a pure Python implementation of the Cyclic Sort algorithm.

For doctests run following command:
python -m doctest -v cyclic_sort.py
or
python3 -m doctest -v cyclic_sort.py
For manual testing run:
python cyclic_sort.py
"""


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.

"""
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.


:param nums: List of integers to be sorted.
:return: The same list sorted in ascending order.

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([3, 5, 2, 1, 4])
[1, 2, 3, 4, 5]
>>> 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.

# Perform cyclic sort
i = 0
while i < len(nums):
# Calculate the correct index for the current element
correct_index = nums[i] - 1
# If the current element is not at its correct position,
# swap it with the element at its correct index
if nums[i] != nums[correct_index]:
nums[i], nums[correct_index] = nums[correct_index], nums[i]
else:
# If the current element is already in its correct position,
# move to the next element
i += 1

return nums


if __name__ == "__main__":
import doctest

doctest.testmod()
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(*cyclic_sort(unsorted), sep=",")