- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcocktailSortAlgorithm.py
More file actions
Latest commit
82 lines (65 loc) · 2.45 KB
/
Copy pathcocktailSortAlgorithm.py
File metadata and controls
82 lines (65 loc) · 2.45 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
importtime
importsortingUtils
fromsortingUtilsimportisSorted, wait
defcocktailSort(array, delay):
swapped=True
start=0
end=len(array) -1
startTime=time.perf_counter()
whilenotisSorted(array) andsortingUtils.sorting:
swapped=False
foriinrange(start, end):
sortingUtils.comparisons+=1
sortingUtils.selectedIndices.clear()
sortingUtils.comparedIndices.clear()
sortingUtils.selectedIndices.append(i)
if (array[i] >array[i+1]):
array[i], array[i+1] =array[i+1], array[i]
sortingUtils.swaps+=1
sortingUtils.comparedIndices.append(i+1)
sortingUtils.swapDataSound.play()
swapped=True
yield
sortingUtils.sortTimeVisual=time.perf_counter() -startTime
wait(delay)
ifnotswapped:
break
swapped=False
end=end-1
foriinrange(end-1, start-1, -1):
sortingUtils.comparisons+=1
sortingUtils.selectedIndices.clear()
sortingUtils.comparedIndices.clear()
sortingUtils.selectedIndices.append(i)
if (array[i] >array[i+1]):
array[i], array[i+1] =array[i+1], array[i]
sortingUtils.swaps+=1
sortingUtils.comparedIndices.append(i+1)
sortingUtils.swapDataSound.play()
swapped=True
yield
sortingUtils.sortTimeVisual=time.perf_counter() -startTime
wait(delay)
start=start+1
defcocktailSortNoVisible(array):
timeArray=array.copy()
startTime=time.perf_counter()
swapped=False
start=0
end=len(array) -1
whilenotisSorted(array) andsortingUtils.sorting:
swapped=False
foriinrange(start, end):
if (timeArray[i] >timeArray[i+1]):
timeArray[i], timeArray[i+1] =timeArray[i+1], timeArray[i]
swapped=True
ifnotswapped:
break
swapped=False
end=end-1
foriinrange(end-1, start-1, -1):
if (timeArray[i] >timeArray[i+1]):
timeArray[i], timeArray[i+1] =timeArray[i+1], timeArray[i]
swapped=True
start=start+1
sortingUtils.sortTime=time.perf_counter() -startTime