Skip to content

Commit 6752e9c

Browse files
authored
Remove boilerplate comments and unused variables (TheAlgorithms#2073)
1 parent 20b21e5 commit 6752e9c

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

sorts/cycle_sort.py

+11-21
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,32 @@ def cycle_sort(array: list) -> list:
1818
>>> cycle_sort([])
1919
[]
2020
"""
21-
ans = 0
21+
array_len = len(array)
22+
for cycle_start in range(0, array_len - 1):
23+
item = array[cycle_start]
2224

23-
# Pass through the array to find cycles to rotate.
24-
for cycleStart in range(0, len(array) - 1):
25-
item = array[cycleStart]
26-
27-
# finding the position for putting the item.
28-
pos = cycleStart
29-
for i in range(cycleStart + 1, len(array)):
25+
pos = cycle_start
26+
for i in range(cycle_start + 1, array_len):
3027
if array[i] < item:
3128
pos += 1
3229

33-
# If the item is already present-not a cycle.
34-
if pos == cycleStart:
30+
if pos == cycle_start:
3531
continue
3632

37-
# Otherwise, put the item there or right after any duplicates.
3833
while item == array[pos]:
3934
pos += 1
40-
array[pos], item = item, array[pos]
41-
ans += 1
42-
43-
# Rotate the rest of the cycle.
44-
while pos != cycleStart:
4535

46-
# Find where to put the item.
47-
pos = cycleStart
48-
for i in range(cycleStart + 1, len(array)):
36+
array[pos], item = item, array[pos]
37+
while pos != cycle_start:
38+
pos = cycle_start
39+
for i in range(cycle_start + 1, array_len):
4940
if array[i] < item:
5041
pos += 1
5142

52-
# Put the item there or right after any duplicates.
5343
while item == array[pos]:
5444
pos += 1
45+
5546
array[pos], item = item, array[pos]
56-
ans += 1
5747

5848
return array
5949

0 commit comments

Comments
 (0)