Skip to content

Commit 409ae68

Browse files
9967hangoswami-rahul
authored andcommitted
Created cycle_sort.py in algorithms/sort (keon#338)
* Create cycle_sort.py * Update test_sort.py * Update __init__.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md
1 parent f9f8746 commit 409ae68

File tree

8 files changed

+57
-0
lines changed

8 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ If you want to uninstall algorithms, it is as simple as:
230230
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
231231
- [comb_sort](algorithms/sort/comb_sort.py)
232232
- [counting_sort](algorithms/sort/counting_sort.py)
233+
- [cycle_sort](algorithms/sort/cycle_sort.py)
233234
- [gnome_sort](algorithms/sort/gnome_sort.py)
234235
- [heap_sort](algorithms/sort/heap_sort.py)
235236
- [insertion_sort](algorithms/sort/insertion_sort.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pip3 uninstall -y algorithms
216216
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
217217
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
218218
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
219+
- [cycle_sort](algorithms/sort/cycle_sort.py)
219220
- [gnome_sort](algorithms/sort/gnome_sort.py)
220221
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)
221222
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
237237
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
238238
- [comb_sort](algorithms/sort/comb_sort.py)
239239
- [counting_sort](algorithms/sort/counting_sort.py)
240+
- [cycle_sort](algorithms/sort/cycle_sort.py)
240241
- [gnome_sort](algorithms/sort/gnome_sort.py)
241242
- [heap_sort](algorithms/sort/heap_sort.py)
242243
- [insertion_sort](algorithms/sort/insertion_sort.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ if __name__ == "__main__":
231231
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
232232
- [comb_sort](algorithms/sort/comb_sort.py)
233233
- [counting_sort](algorithms/sort/counting_sort.py)
234+
- [cycle_sort](algorithms/sort/cycle_sort.py)
234235
- [gnome_sort](algorithms/sort/gnome_sort.py)
235236
- [heap_sort](algorithms/sort/heap_sort.py)
236237
- [insertion_sort](algorithms/sort/insertion_sort.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ if __name__ == "__main__":
227227
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
228228
- [comb_sort](algorithms/sort/comb_sort.py)
229229
- [counting_sort](algorithms/sort/counting_sort.py)
230+
- [cycle_sort](algorithms/sort/cycle_sort.py)
230231
- [gnome_sort](algorithms/sort/gnome_sort.py)
231232
- [heap_sort](algorithms/sort/heap_sort.py)
232233
- [insertion_sort](algorithms/sort/insertion_sort.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .bubble_sort import *
44
from .comb_sort import *
55
from .counting_sort import *
6+
from .cycle_sort import *
67
from .heap_sort import *
78
from .insertion_sort import *
89
from .merge_sort import *

algorithms/sort/cycle_sort.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def cycle_sort(arr):
2+
"""
3+
cycle_sort
4+
This is based on the idea that the permutations to be sorted
5+
can be decomposed into cycles,
6+
and the results can be individually sorted by cycling.
7+
8+
reference: https://en.wikipedia.org/wiki/Cycle_sort
9+
10+
Average time complexity : O(N^2)
11+
Worst case time complexity : O(N^2)
12+
"""
13+
len_arr = len(arr)
14+
# Finding cycle to rotate.
15+
for cur in range(len_arr - 1):
16+
item = arr[cur]
17+
18+
# Finding an indx to put items in.
19+
index = cur
20+
for i in range(cur + 1, len_arr):
21+
if arr[i] < item:
22+
index += 1
23+
24+
# Case of there is not a cycle
25+
if index == cur:
26+
continue
27+
28+
# Putting the item immediately right after the duplicate item or on the right.
29+
while item == arr[index]:
30+
index += 1
31+
arr[index], item = item, arr[index]
32+
33+
# Rotating the remaining cycle.
34+
while index != cur:
35+
36+
# Finding where to put the item.
37+
index = cur
38+
for i in range(cur + 1, len_arr):
39+
if arr[i] < item:
40+
index += 1
41+
42+
# After item is duplicated, put it in place or put it there.
43+
while item == arr[index]:
44+
index += 1
45+
arr[index], item = item, arr[index]
46+
return arr

tests/test_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
bubble_sort,
55
comb_sort,
66
counting_sort,
7+
cycle_sort,
78
max_heap_sort, min_heap_sort,
89
insertion_sort,
910
merge_sort,
@@ -47,6 +48,10 @@ def test_counting_sort(self):
4748
self.assertEqual([-1232, -65, -57, -23, -5, -1],
4849
counting_sort([-1, -5, -65, -23, -57, -1232]))
4950

51+
def test_cycle_sort(self):
52+
self.assertEqual([1, 5, 23, 57, 65, 1232],
53+
cycle_sort([1, 5, 65, 23, 57, 1232]))
54+
5055
def test_heap_sort(self):
5156
self.assertEqual([1, 5, 23, 57, 65, 1232],
5257
max_heap_sort([1, 5, 65, 23, 57, 1232]))

0 commit comments

Comments
 (0)