Skip to content

Commit 749afca

Browse files
Improve docstrings in bubble_sort.py to include complexity analysis
1 parent e3b01ec commit 749afca

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

sorts/bubble_sort.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
55
"""Pure implementation of bubble sort algorithm in Python
66
7+
Time Complexity: O(n^2) - worst and average case
8+
Time Complexity: O(n) - best case (already sorted, early exit via swapped flag)
9+
Space Complexity: O(1) - sorts in place, no extra memory used
10+
711
:param collection: some mutable ordered collection with heterogeneous
812
comparable items inside
913
:return: the same collection ordered in ascending order
@@ -62,6 +66,9 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
6266

6367
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
6468
"""It is similar iterative bubble sort but recursive.
69+
Time Complexity: O(n^2) - worst and average case
70+
Time Complexity: O(n) - best case (already sorted)
71+
Space Complexity: O(n) - recursive call stack uses extra memory
6572
6673
:param collection: mutable ordered sequence of elements
6774
:return: the same list in ascending order

0 commit comments

Comments
 (0)