Skip to content

Commit 93c6183

Browse files
Update cyclic_sort.py
1 parent 73a511a commit 93c6183

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

sorts/cyclic_sort.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ def cyclic_sort(nums: list[int]) -> list[int]:
2929
"""
3030

3131
# Perform cyclic sort
32-
for index in range(len(nums)):
32+
index = 0
33+
while index < len(nums):
3334
# Calculate the correct index for the current element
3435
correct_index = nums[index] - 1
3536
# If the current element is not at its correct position,
3637
# swap it with the element at its correct index
3738
if index != correct_index:
3839
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
3944

4045
return nums
4146

0 commit comments

Comments
 (0)