@@ -8,13 +8,10 @@ def cycle_sort(array: list) -> list:
8
8
"""
9
9
>>> cycle_sort([4, 3, 2, 1])
10
10
[1, 2, 3, 4]
11
-
12
11
>>> cycle_sort([-4, 20, 0, -50, 100, -1])
13
12
[-50, -4, -1, 0, 20, 100]
14
-
15
13
>>> cycle_sort([-.1, -.2, 1.3, -.8])
16
14
[-0.8, -0.2, -0.1, 1.3]
17
-
18
15
>>> cycle_sort([])
19
16
[]
20
17
"""
@@ -29,7 +26,12 @@ def cycle_sort(array: list) -> list:
29
26
30
27
if pos == cycle_start :
31
28
continue
32
-
29
+
30
+ """
31
+ the place that the current element should be
32
+ switched to are already occupied by element with
33
+ same value
34
+ """
33
35
while item == array [pos ]:
34
36
pos += 1
35
37
@@ -39,7 +41,11 @@ def cycle_sort(array: list) -> list:
39
41
for i in range (cycle_start + 1 , array_len ):
40
42
if array [i ] < item :
41
43
pos += 1
42
-
44
+ """
45
+ In the second turn of switching, the place that
46
+ the current element should be switched to are already
47
+ occupied by element with same value
48
+ """
43
49
while item == array [pos ]:
44
50
pos += 1
45
51
@@ -49,5 +55,7 @@ def cycle_sort(array: list) -> list:
49
55
50
56
51
57
if __name__ == "__main__" :
58
+ assert cycle_sort ([3 , 3 , 3 , 3 , 2 ]) == [2 , 3 , 3 , 3 , 3 ]
59
+ assert cycle_sort ([3 , 2 , 3 , 3 , 3 , 2 , 1 ]) == [1 , 2 , 2 , 3 , 3 , 3 , 3 ]
52
60
assert cycle_sort ([4 , 5 , 3 , 2 , 1 ]) == [1 , 2 , 3 , 4 , 5 ]
53
- assert cycle_sort ([0 , 1 , - 10 , 15 , 2 , - 2 ]) == [- 10 , - 2 , 0 , 1 , 2 , 15 ]
61
+ assert cycle_sort ([0 , 1 , - 10 , 15 , 2 , - 2 ]) == [- 10 , - 2 , 0 , 1 , 2 , 15 ]
0 commit comments