We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 73a511a commit 93c6183Copy full SHA for 93c6183
sorts/cyclic_sort.py
@@ -29,13 +29,18 @@ def cyclic_sort(nums: list[int]) -> list[int]:
29
"""
30
31
# Perform cyclic sort
32
- for index in range(len(nums)):
+ index = 0
33
+ while index < len(nums):
34
# Calculate the correct index for the current element
35
correct_index = nums[index] - 1
36
# If the current element is not at its correct position,
37
# swap it with the element at its correct index
38
if index != correct_index:
39
nums[index], nums[correct_index] = nums[correct_index], nums[index]
40
+ else:
41
+ # If the current element is already in its correct position,
42
+ # move to the next element
43
+ index += 1
44
45
return nums
46
0 commit comments