Skip to content

Commit 69f7f32

Browse files
Akash-Jambulkarpre-commit-ci[bot]cclauss
authored
Update cocktail_shaker_sort.py (#10987)
* Update cocktail_shaker_sort.py Added a docstring with clear explanations of the function and its parameters. Changed variable names i, start, and end for better readability. Improved comments to describe the purpose of each section of the algorithm. Adjusted the loop ranges to make the code more concise and readable. Removed redundant comments and variable assignments. Provided a clear message when printing the sorted list. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update cocktail_shaker_sort.py * typing: ignore[operator] * Update cocktail_shaker_sort.py * Update cocktail_shaker_sort.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 29b8ccd commit 69f7f32

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

sorts/cocktail_shaker_sort.py

+37-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
1-
""" https://en.wikipedia.org/wiki/Cocktail_shaker_sort """
1+
"""
2+
An implementation of the cocktail shaker sort algorithm in pure Python.
23
4+
https://en.wikipedia.org/wiki/Cocktail_shaker_sort
5+
"""
36

4-
def cocktail_shaker_sort(unsorted: list) -> list:
7+
8+
def cocktail_shaker_sort(arr: list[int]) -> list[int]:
59
"""
6-
Pure implementation of the cocktail shaker sort algorithm in Python.
10+
Sorts a list using the Cocktail Shaker Sort algorithm.
11+
12+
:param arr: List of elements to be sorted.
13+
:return: Sorted list.
14+
715
>>> cocktail_shaker_sort([4, 5, 2, 1, 2])
816
[1, 2, 2, 4, 5]
9-
1017
>>> cocktail_shaker_sort([-4, 5, 0, 1, 2, 11])
1118
[-4, 0, 1, 2, 5, 11]
12-
1319
>>> cocktail_shaker_sort([0.1, -2.4, 4.4, 2.2])
1420
[-2.4, 0.1, 2.2, 4.4]
15-
1621
>>> cocktail_shaker_sort([1, 2, 3, 4, 5])
1722
[1, 2, 3, 4, 5]
18-
1923
>>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
2024
[-24, -11, -7, -5, -4]
25+
>>> cocktail_shaker_sort(["elderberry", "banana", "date", "apple", "cherry"])
26+
['apple', 'banana', 'cherry', 'date', 'elderberry']
27+
>>> cocktail_shaker_sort((-4, -5, -24, -7, -11))
28+
Traceback (most recent call last):
29+
...
30+
TypeError: 'tuple' object does not support item assignment
2131
"""
22-
for i in range(len(unsorted) - 1, 0, -1):
32+
start, end = 0, len(arr) - 1
33+
34+
while start < end:
2335
swapped = False
2436

25-
for j in range(i, 0, -1):
26-
if unsorted[j] < unsorted[j - 1]:
27-
unsorted[j], unsorted[j - 1] = unsorted[j - 1], unsorted[j]
37+
# Pass from left to right
38+
for i in range(start, end):
39+
if arr[i] > arr[i + 1]:
40+
arr[i], arr[i + 1] = arr[i + 1], arr[i]
2841
swapped = True
2942

30-
for j in range(i):
31-
if unsorted[j] > unsorted[j + 1]:
32-
unsorted[j], unsorted[j + 1] = unsorted[j + 1], unsorted[j]
43+
if not swapped:
44+
break
45+
46+
end -= 1 # Decrease the end pointer after each pass
47+
48+
# Pass from right to left
49+
for i in range(end, start, -1):
50+
if arr[i] < arr[i - 1]:
51+
arr[i], arr[i - 1] = arr[i - 1], arr[i]
3352
swapped = True
3453

3554
if not swapped:
3655
break
37-
return unsorted
56+
57+
start += 1 # Increase the start pointer after each pass
58+
59+
return arr
3860

3961

4062
if __name__ == "__main__":

0 commit comments

Comments
 (0)