Created cycle_sort.py in algorithms/sort by 9967han · Pull Request #338 · keon/algorithms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pip3 uninstall -y algorithms
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
- [cycle_sort](algorithms/sort/cycle_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [cycle_sort](algorithms/sort/cycle_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ if __name__ == "__main__":
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [cycle_sort](algorithms/sort/cycle_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ if __name__ == "__main__":
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [cycle_sort](algorithms/sort/cycle_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/sort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .bubble_sort import *
from .comb_sort import *
from .counting_sort import *
from .cycle_sort import *
from .heap_sort import *
from .insertion_sort import *
from .merge_sort import *
Expand Down
46 changes: 46 additions & 0 deletions algorithms/sort/cycle_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def cycle_sort(arr):
"""
cycle_sort
This is based on the idea that the permutations to be sorted
can be decomposed into cycles,
and the results can be individually sorted by cycling.

reference: https://en.wikipedia.org/wiki/Cycle_sort

Average time complexity : O(N^2)
Worst case time complexity : O(N^2)
"""
len_arr = len(arr)
# Finding cycle to rotate.
for cur in range(len_arr - 1):
item = arr[cur]

# Finding an indx to put items in.
index = cur
for i in range(cur + 1, len_arr):
if arr[i] < item:
index += 1

# Case of there is not a cycle
if index == cur:
continue

# Putting the item immediately right after the duplicate item or on the right.
while item == arr[index]:
index += 1
arr[index], item = item, arr[index]

# Rotating the remaining cycle.
while index != cur:

# Finding where to put the item.
index = cur
for i in range(cur + 1, len_arr):
if arr[i] < item:
index += 1

# After item is duplicated, put it in place or put it there.
while item == arr[index]:
index += 1
arr[index], item = item, arr[index]
return arr
5 changes: 5 additions & 0 deletions tests/test_sort.py