-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
""" | ||
Sorts the input list in-place using the Cyclic Sort algorithm. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
: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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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([]) | ||
[] | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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=",") |
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 thenums
and the method output.