• Cocktail Shaker Sort is a variation of the Bubble Sort algorithm. Like Bubble Sort, Cocktail Shaker Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

    Time Complexity

    • Best Case: O(n) - This is when the list is allready sorted. The algorithm will only need to traverse the list once in each direction without making any swaps.

    • Average Case: O(n^2) - On average, it will need to traverse and compare elements multiple times.

    • Worst Case: O(n^2) - Similar to Bubble Sort worst case scenario, where the list is in reverse order.

    Space Complexity: O(1) - it sorts list in place and requires constant amount of extra space regardless of the input size.

    Type Parameters

    • TElement extends string | number

    Parameters

    • arr: TElement[]

      unsorted array

    Returns TElement[]

    sorted array

    Function

    cocktailShakerSort

Generated using TypeDoc