an implementation of the cocktail shaker sort algorithm in pure python https en wikipedia orgwikicocktailshakersort sorts a list using the cocktail shaker sort algorithm param arr list of elements to be sorted return sorted list cocktailshakersort4 5 2 1 2 1 2 2 4 5 cocktailshakersort4 5 0 1 2 11 4 0 1 2 5 11 cocktailshakersort0 1 2 4 4 4 2 2 2 4 0 1 2 2 4 4 cocktailshakersort1 2 3 4 5 1 2 3 4 5 cocktailshakersort4 5 24 7 11 24 11 7 5 4 cocktailshakersortelderberry banana date apple cherry apple banana cherry date elderberry cocktailshakersort4 5 24 7 11 traceback most recent call last typeerror tuple object does not support item assignment pass from left to right pass from right to left 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 pass from left to right decrease the end pointer after each pass pass from right to left increase the start pointer after each pass
def cocktail_shaker_sort(arr: list[int]) -> list[int]:
    
    start, end = 0, len(arr) - 1

    while start < end:
        swapped = False

        
        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  

        
        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  

    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) = }")