From 8dfea512235161403d1894950940e9a3f6ece580 Mon Sep 17 00:00:00 2001
From: Akash_Jambulkar <97665573+Akash-Jambulkar@users.noreply.github.com>
Date: Thu, 26 Oct 2023 13:56:29 +0530
Subject: [PATCH 1/6] 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.
---
 sorts/cocktail_shaker_sort.py | 47 ++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index b738ff31d768..8a57cf472581 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -1,8 +1,5 @@
 """ https://en.wikipedia.org/wiki/Cocktail_shaker_sort """
-
-
-def cocktail_shaker_sort(unsorted: list) -> list:
-    """
+"""
     Pure implementation of the cocktail shaker sort algorithm in Python.
     >>> cocktail_shaker_sort([4, 5, 2, 1, 2])
     [1, 2, 2, 4, 5]
@@ -18,28 +15,54 @@ def cocktail_shaker_sort(unsorted: list) -> list:
 
     >>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
     [-24, -11, -7, -5, -4]
+"""
+def cocktail_shaker_sort(arr):
+    """
+    Sorts a list using the Cocktail Shaker Sort algorithm.
+    
+    :param arr: List of elements to be sorted.
+    :return: Sorted list.
     """
-    for i in range(len(unsorted) - 1, 0, -1):
+    start, end = 0, len(arr) - 1
+
+    while start < end:
         swapped = False
 
-        for j in range(i, 0, -1):
-            if unsorted[j] < unsorted[j - 1]:
-                unsorted[j], unsorted[j - 1] = unsorted[j - 1], unsorted[j]
+        # 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
 
-        for j in range(i):
-            if unsorted[j] > unsorted[j + 1]:
-                unsorted[j], unsorted[j + 1] = unsorted[j + 1], unsorted[j]
+        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
-    return unsorted
 
+        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(",")]
+    sorted_list = cocktail_shaker_sort(unsorted)
+    print(f"Sorted list: {sorted_list}")
+
+
     doctest.testmod()
     user_input = input("Enter numbers separated by a comma:\n").strip()
     unsorted = [int(item) for item in user_input.split(",")]

From 31b040033614ecbddc0bc6b49476ef0bbc17428f Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Thu, 26 Oct 2023 08:34:03 +0000
Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 sorts/cocktail_shaker_sort.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index 8a57cf472581..14fc95cd139d 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -16,10 +16,12 @@
     >>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
     [-24, -11, -7, -5, -4]
 """
+
+
 def cocktail_shaker_sort(arr):
     """
     Sorts a list using the Cocktail Shaker Sort algorithm.
-    
+
     :param arr: List of elements to be sorted.
     :return: Sorted list.
     """
@@ -52,17 +54,17 @@ def cocktail_shaker_sort(arr):
 
     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(",")]
     sorted_list = cocktail_shaker_sort(unsorted)
     print(f"Sorted list: {sorted_list}")
 
-
     doctest.testmod()
     user_input = input("Enter numbers separated by a comma:\n").strip()
     unsorted = [int(item) for item in user_input.split(",")]

From d5063fcfac1bc54180d50c32ba985aeb5ea0b70a Mon Sep 17 00:00:00 2001
From: Christian Clauss <cclauss@me.com>
Date: Thu, 26 Oct 2023 11:20:09 +0200
Subject: [PATCH 3/6] Update cocktail_shaker_sort.py

---
 sorts/cocktail_shaker_sort.py | 42 ++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 23 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index 14fc95cd139d..8d174baea6f5 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -1,29 +1,32 @@
-""" https://en.wikipedia.org/wiki/Cocktail_shaker_sort """
 """
-    Pure implementation of the cocktail shaker sort algorithm in Python.
-    >>> 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]
+An implementation of the cocktail shaker sort algorithm in pure Python.
 
-    >>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
-    [-24, -11, -7, -5, -4]
+https://en.wikipedia.org/wiki/Cocktail_shaker_sort
 """
+from typing import TypeVar
+
+T = TypeVar("T")
 
 
-def cocktail_shaker_sort(arr):
+def cocktail_shaker_sort(arr: list[T]) -> list[T]:
     """
     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"]
     """
     start, end = 0, len(arr) - 1
 
@@ -58,13 +61,6 @@ def cocktail_shaker_sort(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(",")]
-    sorted_list = cocktail_shaker_sort(unsorted)
-    print(f"Sorted list: {sorted_list}")
-
     doctest.testmod()
     user_input = input("Enter numbers separated by a comma:\n").strip()
     unsorted = [int(item) for item in user_input.split(",")]

From 9b4f06b3e1314de758c6f290928549c452daa39a Mon Sep 17 00:00:00 2001
From: Christian Clauss <cclauss@me.com>
Date: Thu, 26 Oct 2023 11:34:12 +0200
Subject: [PATCH 4/6] typing: ignore[operator]

---
 sorts/cocktail_shaker_sort.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index 8d174baea6f5..b4c1b05cb128 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -23,10 +23,14 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
     [-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))
+    >>> 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
 
@@ -35,7 +39,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from left to right
         for i in range(start, end):
-            if arr[i] > arr[i + 1]:
+            if arr[i] > arr[i + 1]:  # typing: ignore[operator]
                 arr[i], arr[i + 1] = arr[i + 1], arr[i]
                 swapped = True
 
@@ -46,7 +50,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from right to left
         for i in range(end, start, -1):
-            if arr[i] < arr[i - 1]:
+            if arr[i] < arr[i - 1]:  # typing: ignore[operator]
                 arr[i], arr[i - 1] = arr[i - 1], arr[i]
                 swapped = True
 

From 41c0994ed7dff5aecab7dcce69450263ce2109d5 Mon Sep 17 00:00:00 2001
From: Christian Clauss <cclauss@me.com>
Date: Thu, 26 Oct 2023 13:19:08 +0200
Subject: [PATCH 5/6] Update cocktail_shaker_sort.py

---
 sorts/cocktail_shaker_sort.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index b4c1b05cb128..726cedd21a40 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -26,7 +26,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
     >>> 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"]
+    ['apple', 'banana', 'cherry', 'date', 'elderberry']
     >>> cocktail_shaker_sort((-4, -5, -24, -7, -11))
     Traceback (most recent call last):
         ...
@@ -39,7 +39,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from left to right
         for i in range(start, end):
-            if arr[i] > arr[i + 1]:  # typing: ignore[operator]
+            if arr[i] > arr[i + 1]:  # typing: ignore
                 arr[i], arr[i + 1] = arr[i + 1], arr[i]
                 swapped = True
 
@@ -50,7 +50,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from right to left
         for i in range(end, start, -1):
-            if arr[i] < arr[i - 1]:  # typing: ignore[operator]
+            if arr[i] < arr[i - 1]:  # typing: ignore
                 arr[i], arr[i - 1] = arr[i - 1], arr[i]
                 swapped = True
 

From 99d8782c640369e6ec81cef27eaf1e799d9719f7 Mon Sep 17 00:00:00 2001
From: Christian Clauss <cclauss@me.com>
Date: Thu, 26 Oct 2023 13:21:39 +0200
Subject: [PATCH 6/6] Update cocktail_shaker_sort.py

---
 sorts/cocktail_shaker_sort.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py
index 726cedd21a40..de126426d986 100644
--- a/sorts/cocktail_shaker_sort.py
+++ b/sorts/cocktail_shaker_sort.py
@@ -3,12 +3,9 @@
 
 https://en.wikipedia.org/wiki/Cocktail_shaker_sort
 """
-from typing import TypeVar
 
-T = TypeVar("T")
 
-
-def cocktail_shaker_sort(arr: list[T]) -> list[T]:
+def cocktail_shaker_sort(arr: list[int]) -> list[int]:
     """
     Sorts a list using the Cocktail Shaker Sort algorithm.
 
@@ -39,7 +36,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from left to right
         for i in range(start, end):
-            if arr[i] > arr[i + 1]:  # typing: ignore
+            if arr[i] > arr[i + 1]:
                 arr[i], arr[i + 1] = arr[i + 1], arr[i]
                 swapped = True
 
@@ -50,7 +47,7 @@ def cocktail_shaker_sort(arr: list[T]) -> list[T]:
 
         # Pass from right to left
         for i in range(end, start, -1):
-            if arr[i] < arr[i - 1]:  # typing: ignore
+            if arr[i] < arr[i - 1]:
                 arr[i], arr[i - 1] = arr[i - 1], arr[i]
                 swapped = True