Skip to content

Commit 948d4cb

Browse files
alisatwat3cclauss
andauthored
Improve docstrings in sorts/bubble_sort.py (#14924)
* docs: expand bubble sort docstrings with algorithm explanation Add a concise description of how bubble sort works (repeated adjacent comparisons/swaps until a pass makes no swaps) and note time/space complexity for both the iterative and recursive implementations. No behavior changes; all existing doctests pass. * Fix Ruff 0.16 lint failures * S310 --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 25bcced commit 948d4cb

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

data_structures/binary_tree/inorder_tree_traversal_2022.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def insert(node: BinaryTreeNode | None, new_value: int) -> BinaryTreeNode | None
4343
return node
4444

4545

46-
def inorder(node: None | BinaryTreeNode) -> list[int]: # if node is None,return
46+
def inorder(node: BinaryTreeNode | None) -> list[int]: # if node is None,return
4747
"""
4848
>>> inorder(make_tree())
4949
[6, 10, 14, 15, 20, 25, 60]

sorts/bubble_sort.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
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 the bubble sort algorithm in Python (iterative).
6+
7+
Bubble sort works by repeatedly stepping through the collection,
8+
comparing each pair of adjacent elements and swapping them if they
9+
are in the wrong order. This process repeats, with each full pass
10+
"bubbling" the next-largest unsorted element into its correct
11+
position at the end of the collection, until a full pass completes
12+
with no swaps, at which point the collection is sorted.
13+
14+
Time complexity: O(n) best case (already sorted, thanks to the
15+
early-exit optimization), O(n^2) average and worst case.
16+
Space complexity: O(1) auxiliary (sorts in place).
617
718
:param collection: some mutable ordered collection with heterogeneous
819
comparable items inside
@@ -61,7 +72,19 @@ 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 implementation of the bubble sort algorithm in Python (recursive).
76+
77+
Functionally identical to the iterative version: each call makes a
78+
single pass through the collection, comparing adjacent elements and
79+
swapping any pair that is out of order. If any swap occurred during
80+
the pass, the function calls itself again on the (partially sorted)
81+
collection; once a pass completes with no swaps, the collection is
82+
sorted and the recursion stops.
83+
84+
Time complexity: O(n) best case (already sorted), O(n^2) average and
85+
worst case.
86+
Space complexity: O(1) auxiliary for the sort itself (sorts in place),
87+
though the recursion adds O(n) call-stack frames in the worst case.
6588
6689
:param collection: mutable ordered sequence of elements
6790
:return: the same list in ascending order

web_programming/download_images_from_google_query.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ def download_images_from_google_query(query: str = "dhaka", max_images: int = 5)
8888
opener.addheaders = [
8989
(
9090
"User-Agent",
91-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
92-
" (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582",
91+
(
92+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
93+
" (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
94+
" Edge/18.19582"
95+
),
9396
)
9497
]
9598
urllib.request.install_opener(opener)

0 commit comments

Comments
 (0)