-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcocktailSort.py
More file actions
36 lines (30 loc) · 954 Bytes
/
cocktailSort.py
File metadata and controls
36 lines (30 loc) · 954 Bytes
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
def cocktailSort(array):
comparisons = 0
swaps = 0
n = len(array)
swapped = True
start = 0
end = n-1
while (swapped==True):
swapped = False
for i in range (start, end):
comparisons += 1
if (array[i] > array[i+1]):
swaps += 1
array[i], array[i+1]= array[i+1], array[i]
swapped=True
yield array, i, i + 1, comparisons, swaps
comparisons, swaps = 0, 0
if (swapped==False):
break
swapped = False
end = end-1
for i in range(end-1, start-1,-1):
comparisons += 1
if (array[i] > array[i+1]):
swaps += 1
array[i], array[i+1] = array[i+1], array[i]
swapped = True
yield array, i, i + 1, comparisons, swaps
comparisons, swaps = 0, 0
start = start + 1