diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 4d658a4a12e4..63bc67645f93 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -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 @@ -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