forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
f9f8746
commit 409ae68
Showing
8 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters