forked from me-shaon/Algorithms_Example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCycleSort.py
More file actions
54 lines (41 loc) · 1.4 KB
/
Copy pathCycleSort.py
File metadata and controls
54 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
# source: https://sr.wikipedia.org/wiki/Cikli%C4%8Dno_sortiranje#Algoritam
# Sort an array in place and return the sorted array
def cycleSort(array):
writes = 0
# Loop through the array to find cycles to rotate.
for cycleStart in range(0, len(array) - 1):
item = array[cycleStart]
# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# If the item is already there, this is not a cycle.
if pos == cycleStart:
continue
# Otherwise, put the item there or right after any duplicates.
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
# Rotate the rest of the cycle.
while pos != cycleStart:
# Find where to put the item.
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
# Put the item there or right after any duplicates.
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
return array
# Test For the CycleSort
class TestSuite(unittest.TestCase):
def test_cycleSort(self):
arr = [2, 5, 8, 6, 35, 1, 2, 545, 6, 2, 3, 12, 4]
self.assertEqual([1, 2, 2, 2, 3, 4, 5, 6, 6, 8, 12, 35, 545], cycleSort(arr))
if __name__ == "__main__":
unittest.main()