forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCocktail_Sort.py
51 lines (38 loc) · 1.36 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
# Cocktail Sort using python
def cocktail_Sort(array):
num = len(array)
swap = True
beg = 0
end = num-1
while (swap == True):
# reseting the swapped flag on entering the loop, because it might be true from a previous iteration.
swap = False
# Similar to bubble sort
for i in range (beg, end):
if (array[i] > array[i + 1]) :
array[i], array[i + 1]= array[i + 1], array[i]
swap = True
# if nothing is swapped, then array is sorted.
if (swap == False):
break
# reset the swapped flag
swap = False
# decrement end point by one
end = end-1
# same procedure to apply from right to left
for i in range(end-1, beg-1, -1):
if (array[i] > array[i + 1]):
array[i], array[i + 1] = array[i + 1], array[i]
swap = True
# increment the beg as the smallest number has been placed at its correct position
beg = beg + 1
# Driver code
array=[]
n=int(input("Enter size of the array:\n"))
for i in range(0,n):
temp=int(input("Enter number to be added:\n"))
array.append(temp)
cocktail_Sort(array)
print("Sorted array is:")
for i in range(len(array)):
print (array[i])