Skip to content

Commit bbe81f2

Browse files
authored
Merge pull request deutranium#179 from durgeshahire07/durgesh-cocktailSort
Implemented cocktail sort in Python
2 parents 08286a3 + 5e6878c commit bbe81f2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Basically these are supposed to be my notes, but feel free to use them as you wi
4747
### [Sorting Algorithms](sortingAlgo)
4848
- [Bucket Sort](sortingAlgo/bucketsort)
4949
- [Bubble Sort](sortingAlgo/bubbleSort)
50+
- [Cocktail Sort](sortingAlgo/cocktailSort)
5051
- [Comb Sort](sortingAlgo/combSort)
5152
- [Counting Sort](sortingAlgo/countingSort)
5253
- [Cycle Sort](sortingAlgo/cycleSort)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# cocktail sort function
2+
def cocktailSort(A):
3+
up = range(len(A)-1)
4+
while True:
5+
for indices in (up, reversed(up)):
6+
7+
# reseting the swapped flag
8+
swapped = False
9+
10+
# looping through left to right
11+
for i in indices:
12+
if A[i] > A[i+1]:
13+
A[i], A[i+1] = A[i+1], A[i]
14+
swapped = True
15+
16+
# if noting is swapped means, array is sorted
17+
if not swapped:
18+
return
19+
20+
# initializing the array
21+
array = [25,1,-2,70,100,-10,40,2,0,5]
22+
print("Array before sorting: ",array)
23+
24+
cocktailSort(array)
25+
print("Array after sorting: ",array)

0 commit comments

Comments
 (0)