-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCocktail Sort.py
33 lines (28 loc) · 1.11 KB
/
Cocktail Sort.py
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
"""
Cocktail sort:
Best case: O(n) , if first loop of bubble sort found out no swap action performed, it means list is sorted
Worst case: O(n^2) , looping backward and forward with bubble sort for entire list, until 1 of looping does not perform any swap
"""
def cocktail_sort(list):
sorted = 0
i = 0
j = len(list)-1
while sorted != 1:
swap = 0
for index in range(i,j): #bubble sort forward
if list[index]>list[index+1]:
list[index],list[index+1] = list[index+1],list[index]
swap +=1
if swap == 0: #if no swap action is performed, means list is sorted
sorted = 1
j-=1 #more bigger value will be found as looping forward
swap = 0
for index in range(j,i,-1): #bubble sort backward
if list[index]<list[index-1]:
list[index],list[index-1] = list[index-1],list[index]
swap +=1
print(index)
if swap == 0:
sorted = 1
i+=1 #more smaller value will be found as looping backward
return list