Skip to content

Commit 33488db

Browse files
committed
Improved bubble_sort docstrings by adding time and space complexity
1 parent 791deb4 commit 33488db

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

sorts/bubble_sort.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33

44
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5-
"""Pure implementation of bubble sort algorithm in Python
5+
"""Pure implementation of bubble sort algorithm in Python.
6+
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them
7+
if they are in the wrong order. This process is repeated until the list is sorted.
8+
9+
Time COmplexity:
10+
Worst Case: O(n^2)
11+
Average Case: O(n^2)
12+
Best Case: O(n) (When the list is already sorted and optimized with early stopping)
13+
14+
Space Complexity:
15+
O(1) (in-place sorting)
616
717
:param collection: some mutable ordered collection with heterogeneous
818
comparable items inside
@@ -62,6 +72,13 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
6272

6373
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
6474
"""It is similar iterative bubble sort but recursive.
75+
Time Complexity:
76+
Worst Case: O(n^2)
77+
Average Case: O(n^2)
78+
Best Case: O(n^2) (no early stopping optimization like iterative version)
79+
80+
Space Complexity:
81+
O(n) due to recursion stac
6582
6683
:param collection: mutable ordered sequence of elements
6784
:return: the same list in ascending order

0 commit comments

Comments
 (0)