mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
69f7f3208e
* 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>
69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
"""
|
|
An implementation of the cocktail shaker sort algorithm in pure Python.
|
|
|
|
https://en.wikipedia.org/wiki/Cocktail_shaker_sort
|
|
"""
|
|
|
|
|
|
def cocktail_shaker_sort(arr: list[int]) -> list[int]:
|
|
"""
|
|
Sorts a list using the Cocktail Shaker Sort algorithm.
|
|
|
|
:param arr: List of elements to be sorted.
|
|
:return: Sorted list.
|
|
|
|
>>> cocktail_shaker_sort([4, 5, 2, 1, 2])
|
|
[1, 2, 2, 4, 5]
|
|
>>> cocktail_shaker_sort([-4, 5, 0, 1, 2, 11])
|
|
[-4, 0, 1, 2, 5, 11]
|
|
>>> cocktail_shaker_sort([0.1, -2.4, 4.4, 2.2])
|
|
[-2.4, 0.1, 2.2, 4.4]
|
|
>>> cocktail_shaker_sort([1, 2, 3, 4, 5])
|
|
[1, 2, 3, 4, 5]
|
|
>>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
|
|
[-24, -11, -7, -5, -4]
|
|
>>> cocktail_shaker_sort(["elderberry", "banana", "date", "apple", "cherry"])
|
|
['apple', 'banana', 'cherry', 'date', 'elderberry']
|
|
>>> cocktail_shaker_sort((-4, -5, -24, -7, -11))
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: 'tuple' object does not support item assignment
|
|
"""
|
|
start, end = 0, len(arr) - 1
|
|
|
|
while start < end:
|
|
swapped = False
|
|
|
|
# Pass from left to right
|
|
for i in range(start, end):
|
|
if arr[i] > arr[i + 1]:
|
|
arr[i], arr[i + 1] = arr[i + 1], arr[i]
|
|
swapped = True
|
|
|
|
if not swapped:
|
|
break
|
|
|
|
end -= 1 # Decrease the end pointer after each pass
|
|
|
|
# Pass from right to left
|
|
for i in range(end, start, -1):
|
|
if arr[i] < arr[i - 1]:
|
|
arr[i], arr[i - 1] = arr[i - 1], arr[i]
|
|
swapped = True
|
|
|
|
if not swapped:
|
|
break
|
|
|
|
start += 1 # Increase the start pointer after each pass
|
|
|
|
return arr
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
|
unsorted = [int(item) for item in user_input.split(",")]
|
|
print(f"{cocktail_shaker_sort(unsorted) = }")
|