Skip to content

Commit d0464cc

Browse files
hsi1032goswami-rahul
authored andcommitted
Create cocktail_shaker_sort and add test case (keon#310)
* Create cocktail_shaker_sort.py * Update __init__.py * Update test_sort.py * Update test_sort.py * Update README.md * Update README_CN.md * Update README_CN.md * Update README.md * Update README_GE.md * Update README_JP.md * Update README_KR.md
1 parent 6b662a1 commit d0464cc

File tree

8 files changed

+42
-2
lines changed

8 files changed

+42
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ If you want to uninstall algorithms, it is as simple as:
225225
- [bogo_sort](algorithms/sort/bogo_sort.py)
226226
- [bubble_sort](algorithms/sort/bubble_sort.py)
227227
- [bucket_sort](algorithms/sort/bucket_sort.py)
228+
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
228229
- [comb_sort](algorithms/sort/comb_sort.py)
229230
- [counting_sort](algorithms/sort/counting_sort.py)
230231
- [heap_sort](algorithms/sort/heap_sort.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pip3 uninstall -y algorithms
209209
- [bogo_sort](algorithms/sort/bogo_sort.py)
210210
- [bubble_sort:冒泡排序](algorithms/sort/bubble_sort.py)
211211
- [bucket_sort](algorithms/sort/bucket_sort.py)
212+
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
212213
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
213214
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
214215
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
230230
- [bogo_sort](algorithms/sort/bogo_sort.py)
231231
- [bubble_sort](algorithms/sort/bubble_sort.py)
232232
- [bucket_sort](algorithms/sort/bucket_sort.py)
233+
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
233234
- [comb_sort](algorithms/sort/comb_sort.py)
234235
- [counting_sort](algorithms/sort/counting_sort.py)
235236
- [heap_sort](algorithms/sort/heap_sort.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ if __name__ == "__main__":
224224
- [bogo_sort](algorithms/sort/bogo_sort.py)
225225
- [bubble_sort](algorithms/sort/bubble_sort.py)
226226
- [bucket_sort](algorithms/sort/bucket_sort.py)
227+
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
227228
- [comb_sort](algorithms/sort/comb_sort.py)
228229
- [counting_sort](algorithms/sort/counting_sort.py)
229230
- [heap_sort](algorithms/sort/heap_sort.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ if __name__ == "__main__":
220220
- [sort : 정렬 알고리즘](algorithms/sort)
221221
- [bogo_sort](algorithms/sort/bogo_sort.py)
222222
- [bubble_sort](algorithms/sort/bubble_sort.py)
223+
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
223224
- [comb_sort](algorithms/sort/comb_sort.py)
224225
- [counting_sort](algorithms/sort/counting_sort.py)
225226
- [heap_sort](algorithms/sort/heap_sort.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
from .bucket_sort import *
1212
from .shell_sort import *
1313
from .radix_sort import *
14+
from .cocktail_shaker_sort import *
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def cocktail_shaker_sort(arr):
2+
"""
3+
Cocktail_shaker_sort
4+
Sorting a given array
5+
mutation of bubble sort
6+
7+
reference: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
8+
9+
Worst-case performance: O(N^2)
10+
"""
11+
12+
def swap(i, j):
13+
arr[i], arr[j] = arr[j], arr[i]
14+
15+
n = len(arr)
16+
swapped = True
17+
while swapped:
18+
swapped = False
19+
for i in range(1, n):
20+
if arr[i - 1] > arr[i]:
21+
swap(i - 1, i)
22+
swapped = True
23+
if swapped == False:
24+
return arr
25+
swapped = False
26+
for i in range(n-1,0,-1):
27+
if arr[i - 1] > arr[i]:
28+
swap(i - 1, i)
29+
swapped = True
30+
return arr

tests/test_sort.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
selection_sort,
1111
bucket_sort,
1212
shell_sort,
13-
radix_sort
13+
radix_sort,
14+
cocktail_shaker_sort
1415
)
1516

1617
import unittest
@@ -68,7 +69,10 @@ def test_shell_sort(self):
6869
def test_radix_sort(self):
6970
self.assertEqual([1, 5, 23, 57, 65, 1232],
7071
radix_sort([1, 5, 65, 23, 57, 1232]))
71-
72+
73+
def test_cocktail_shaker_sort(self):
74+
self.assertEqual([1, 5, 23, 57, 65, 1232],
75+
cocktail_shaker_sort([1, 5, 65, 23, 57, 1232]))
7276

7377

7478
if __name__ == "__main__":

0 commit comments

Comments
 (0)