Create cocktail_shaker_sort and add test case by hsi1032 · Pull Request #310 · keon/algorithms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pip3 uninstall -y algorithms
- [sort:排序](algorithms/sort)
- [bubble_sort:冒泡排序](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [sort](algorithms/sort)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ if __name__ == "__main__":
- [sort : ソート](algorithms/sort)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [bucket_sort](algorithms/sort/bucket_sort.py)
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ if __name__ == "__main__":
- [set_covering](algorithms/set/set_covering.py)
- [sort : 정렬 알고리즘](algorithms/sort)
- [bubble_sort](algorithms/sort/bubble_sort.py)
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/sort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .bucket_sort import *
from .shell_sort import *
from .radix_sort import *
from .cocktail_shaker_sort import *
30 changes: 30 additions & 0 deletions algorithms/sort/cocktail_shaker_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def cocktail_shaker_sort(arr):
"""
Cocktail_shaker_sort
Sorting a given array
mutation of bubble sort

reference: https://en.wikipedia.org/wiki/Cocktail_shaker_sort

Worst-case performance: O(N^2)
"""

def swap(i, j):
arr[i], arr[j] = arr[j], arr[i]

n = len(arr)
swapped = True
while swapped:
swapped = False
for i in range(1, n):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
if swapped == False:
return arr
swapped = False
for i in range(n-1,0,-1):
if arr[i - 1] > arr[i]:
swap(i - 1, i)
swapped = True
return arr
8 changes: 6 additions & 2 deletions tests/test_sort.py