Skip to content

Commit 3aabac4

Browse files
jamesjames
authored andcommitted
docs(#14593): add complexity analysis to bubble_sort.py docstrings
1 parent 6c04620 commit 3aabac4

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

sorts/bubble_sort.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
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+
7+
Repeatedly steps through the list, compares adjacent elements, and swaps
8+
them if they are in the wrong order. The pass through the list is repeated
9+
until the list is sorted. Stops early if no swaps are made in a pass.
610
711
:param collection: some mutable ordered collection with heterogeneous
812
comparable items inside
913
:return: the same collection ordered in ascending order
1014
15+
Time Complexity:
16+
Best Case: O(n) — already sorted (early exit via swapped flag)
17+
Average Case: O(n²)
18+
Worst Case: O(n²)
19+
Space Complexity: O(1) — in-place sorting
20+
Stable: Yes
21+
1122
Examples:
1223
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
1324
[0, 2, 2, 3, 5]
@@ -61,11 +72,21 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
6172

6273

6374
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
64-
"""It is similar iterative bubble sort but recursive.
75+
"""Pure recursive implementation of bubble sort algorithm in Python.
76+
77+
Performs one pass to bubble the largest unsorted element to the end,
78+
then recursively sorts the remaining elements. Stops when no swaps occur.
6579
6680
:param collection: mutable ordered sequence of elements
6781
:return: the same list in ascending order
6882
83+
Time Complexity:
84+
Best Case: O(n²) — recursive call stack prevents early exit
85+
Average Case: O(n²)
86+
Worst Case: O(n²)
87+
Space Complexity: O(n) — recursive call stack depth
88+
Stable: Yes
89+
6990
Examples:
7091
>>> bubble_sort_recursive([0, 5, 2, 3, 2])
7192
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)