Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion sorts/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@


def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
"""Pure implementation of bubble sort algorithm in Python
"""Pure implementation of bubble sort algorithm in Python.
Bubble sort repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
This process is repeated until the list is sorted.

Time Complexity:
Worst Case: O(n^2)
Average Case: O(n^2)
Best Case: O(n) (When the list is already sorted and
optimized with early stopping)

Space Complexity:
O(1) (in-place sorting)

:param collection: some mutable ordered collection with heterogeneous
comparable items inside
Expand Down Expand Up @@ -62,6 +74,14 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:

def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
"""It is similar iterative bubble sort but recursive.
Time Complexity:
Worst Case: O(n^2)
Average Case: O(n^2)
Best Case: O(n^2) (no early stopping
optimization like iterative version)

Space Complexity:
O(n) due to recursion stac

:param collection: mutable ordered sequence of elements
:return: the same list in ascending order
Expand Down