Skip to content

Commit e5fcd6b

Browse files
docs: add complexity analysis to bubble_sort docstrings
Added time and space complexity analysis for both iterative and recursive bubble sort implementations.
1 parent 6c04620 commit e5fcd6b

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

sorts/bubble_sort.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
55
"""Pure implementation of bubble sort algorithm in Python
66
77
:param collection: some mutable ordered collection with heterogeneous
8-
comparable items inside
8+
comparable items inside
99
:return: the same collection ordered in ascending order
1010
11+
Time Complexity:
12+
- Worst Case: O(n^2)
13+
- Average Case: O(n^2)
14+
- Best Case: O(n)
15+
16+
Space Complexity: O(1) auxiliary space
17+
1118
Examples:
1219
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
1320
[0, 2, 2, 3, 5]
@@ -66,6 +73,13 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
6673
:param collection: mutable ordered sequence of elements
6774
:return: the same list in ascending order
6875
76+
Time Complexity:
77+
- Worst Case: O(n^2)
78+
- Average Case: O(n^2)
79+
- Best Case: O(n)
80+
81+
Space Complexity: O(n) auxiliary stack space due to recursion
82+
6983
Examples:
7084
>>> bubble_sort_recursive([0, 5, 2, 3, 2])
7185
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)