Skip to content

Commit a4a0cfb

Browse files
author
Naresh Koundel
committed
docs: Improve bubble sort documentation with beginner-friendly explanations
- Add comprehensive algorithm explanation and complexity analysis - Include step-by-step inline comments for iterative implementation - Add detailed comments for recursive implementation - Explain the algorithm flow, comparison logic, and optimization techniques - Include complexity information (Time: O(n²), Space: O(1) for iterative)
1 parent 841e947 commit a4a0cfb

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

sorts/bubble_sort.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
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+
BUBBLE SORT EXPLANATION:
8+
Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
9+
compares adjacent elements, and swaps them if they are in the wrong order.
10+
The largest elements "bubble up" to the end of the list with each pass.
11+
12+
Algorithm Steps:
13+
1. Start from the beginning of the list
14+
2. Compare each pair of adjacent elements
15+
3. If the left element is greater than the right, swap them
16+
4. After each complete pass, the largest unsorted element reaches its correct position
17+
5. Repeat until no more swaps are needed
18+
19+
Time Complexity: O(n²) in worst and average cases, O(n) in best case
20+
Space Complexity: O(1) - sorts in-place
621
722
:param collection: some mutable ordered collection with heterogeneous
823
comparable items inside
@@ -48,20 +63,48 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
4863
>>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
4964
True
5065
"""
66+
# Get the length of the collection to know how many passes we need
5167
length = len(collection)
68+
69+
# Outer loop: iterate from the end of the list backwards
70+
# After each pass, the position 'i' will have its final, sorted element
71+
# so we reduce the range each time (optimization)
5272
for i in reversed(range(length)):
73+
# Flag to track if any swaps occurred in this pass
74+
# If no swaps happen, the list is already sorted and we can stop early
5375
swapped = False
76+
77+
# Inner loop: compare adjacent elements and swap if needed
78+
# We only compare up to position 'i' since elements after 'i' are already sorted
5479
for j in range(i):
80+
# Compare current element with the next element
5581
if collection[j] > collection[j + 1]:
82+
# If elements are in wrong order, swap them
5683
swapped = True
5784
collection[j], collection[j + 1] = collection[j + 1], collection[j]
85+
86+
# Optimization: if no swaps occurred in this pass, the list is sorted
87+
# No need to continue iterating
5888
if not swapped:
59-
break # Stop iteration if the collection is sorted.
89+
break
90+
6091
return collection
6192

6293

6394
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
64-
"""It is similar iterative bubble sort but recursive.
95+
"""Recursive implementation of bubble sort algorithm.
96+
97+
This version uses recursion instead of iteration to perform bubble sort.
98+
Instead of using loops, it calls itself repeatedly until the list is sorted.
99+
100+
How it works:
101+
1. In each recursive call, perform one complete pass through the list
102+
2. Compare and swap adjacent elements that are out of order
103+
3. After one pass, the largest unsorted element reaches its correct position
104+
4. Recursively call the function again until no swaps occur (list is sorted)
105+
106+
Time Complexity: O(n²)
107+
Space Complexity: O(n) - due to the call stack from recursion
65108
66109
:param collection: mutable ordered sequence of elements
67110
:return: the same list in ascending order
@@ -102,13 +145,23 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
102145
>>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
103146
True
104147
"""
148+
# Get the length of the collection
105149
length = len(collection)
150+
151+
# Track whether any swaps occurred in this pass
152+
# If no swaps happen, the list is already sorted
106153
swapped = False
154+
155+
# Perform one complete pass: compare and swap adjacent elements
107156
for i in range(length - 1):
157+
# Compare current element with the next element
108158
if collection[i] > collection[i + 1]:
159+
# Swap them if they're in wrong order
109160
collection[i], collection[i + 1] = collection[i + 1], collection[i]
110161
swapped = True
111162

163+
# Base case: if no swaps occurred, the list is sorted - return it
164+
# Recursive case: if swaps occurred, call the function again to make another pass
112165
return collection if not swapped else bubble_sort_recursive(collection)
113166

114167

0 commit comments

Comments
 (0)