Skip to content

Commit e29ba6d

Browse files
authored
Merge pull request #39 from IainMcl/CocktailSort
Cocktail sort
2 parents 76133af + 517cee1 commit e29ba6d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def cocktailSort(unsorted):
2+
swapped = True
3+
start = 0
4+
end = len(unsorted) -1
5+
while swapped:
6+
swapped = False
7+
# forward pass
8+
for i in range(start, end):
9+
if unsorted[i] > unsorted[i+1]:
10+
swapped = True
11+
unsorted[i], unsorted[i+1] = unsorted[i+1], unsorted[i]
12+
13+
end -= 1
14+
15+
# backward pass
16+
for i in range(end-1, start-1, -1):
17+
if unsorted[i] > unsorted[i+1]:
18+
swapped = True
19+
unsorted[i], unsorted[i+1] = unsorted[i+1], unsorted[i]
20+
21+
start += 1
22+
print(unsorted)
23+
24+
25+
def test():
26+
arr = [3,4,8,76,45,3,1,0,5,-34, 23]
27+
cocktailSort(arr)
28+
29+
30+
if __name__ == "__main__":
31+
test()

0 commit comments

Comments
 (0)