Skip to content
Closed
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
8 changes: 8 additions & 0 deletions sorts/cyclic_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def cyclic_sort(nums: list[int]) -> list[int]:

:param nums: List of n integers from 1 to n to be sorted.
:return: The same list sorted in ascending order.
:raises ValueError: If nums contains non-integer or negative values.

Time complexity: O(n), where n is the number of integers in the list.

Expand All @@ -27,8 +28,15 @@ def cyclic_sort(nums: list[int]) -> list[int]:
[]
>>> cyclic_sort([3, 5, 2, 1, 4])
[1, 2, 3, 4, 5]
>>> cyclic_sort([3, 5, -2, 1, 4])
Traceback (most recent call last):
...
ValueError: All values must be positive integers
"""

if any(not isinstance(x, int) or x <= 0 for x in nums):
raise ValueError("All values must be positive integers")

# Perform cyclic sort
index = 0
while index < len(nums):
Expand Down