-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcocktail sort.py
94 lines (73 loc) · 2.12 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
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
83
84
85
86
87
88
89
90
91
92
93
94
import random
import time
N = 10000
def std_cocktail_sort (x):
left = 0
right = len(x)-1
while left <= right:
for i in range(left, right):
if x[i] > x[i+1]:
x[i], x[i+1] = x[i+1], x[i]
right = right - 1
for i in range(right, left, -1):
if x[i-1] > x[i]:
x[i-1], x[i] = x[i], x[i-1]
left = left + 1
return x
def opt_cocktail_sort(x):
for i in range(len(x)//2):
swap = False
for j in range(1+i, len(x)-i):
if x[j] < x[j-1]:
x[j], x[j - 1] = x[j - 1], x[j]
swap = True
if not swap:
break
swap = False
for j in range(len(x)-i-1, i, -1):
if x[j] < x[j-1]:
x[j], x[j - 1] = x[j - 1], x[j]
swap = True
if not swap:
break
return x
if __name__ == "__main__":
a = []
b = []
c = []
for i in range(N):
a.append(i)
b.append(N-i)
c.append(random.randint(-N, N))
print("NO OPTIMIZED std_cocktail_sort(x)")
print("\n...SORTING ARRAY...")
st = time.time()
na = std_cocktail_sort(a)
end = time.time()
print("\nTIME:", (end - st), " sec")
print("\n...REVERSE ARRAY...")
st = time.time()
nb = std_cocktail_sort(b)
end = time.time()
print( "\nTIME:", (end - st), " sec")
print("\n...RANDOM ARRAY...")
st = time.time()
nc = std_cocktail_sort(c)
end = time.time()
print("\nTIME:", (end - st), " sec")
print("\n\nOPTIMIZED opt_cocktail_sort(x)")
print("\n...SORTING ARRAY...")
st = time.time()
na = opt_cocktail_sort(a)
end = time.time()
print("\nTIME:", (end - st), " sec")
print("\n...REVERSE ARRAY...")
st = time.time()
nb = opt_cocktail_sort(b)
end = time.time()
print("\nTIME:", (end - st), " sec")
print("\n...RANDOM ARRAY...")
st = time.time()
nc = opt_cocktail_sort(c)
end = time.time()
print("\nTIME:", (end - st), " sec")