|
2 | 2 |
|
3 | 3 |
|
4 | 4 | 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 |
6 | 21 |
|
7 | 22 | :param collection: some mutable ordered collection with heterogeneous |
8 | 23 | comparable items inside |
@@ -48,20 +63,48 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]: |
48 | 63 | >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg) |
49 | 64 | True |
50 | 65 | """ |
| 66 | + # Get the length of the collection to know how many passes we need |
51 | 67 | 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) |
52 | 72 | 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 |
53 | 75 | 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 |
54 | 79 | for j in range(i): |
| 80 | + # Compare current element with the next element |
55 | 81 | if collection[j] > collection[j + 1]: |
| 82 | + # If elements are in wrong order, swap them |
56 | 83 | swapped = True |
57 | 84 | 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 |
58 | 88 | if not swapped: |
59 | | - break # Stop iteration if the collection is sorted. |
| 89 | + break |
| 90 | + |
60 | 91 | return collection |
61 | 92 |
|
62 | 93 |
|
63 | 94 | 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 |
65 | 108 |
|
66 | 109 | :param collection: mutable ordered sequence of elements |
67 | 110 | :return: the same list in ascending order |
@@ -102,13 +145,23 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]: |
102 | 145 | >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg) |
103 | 146 | True |
104 | 147 | """ |
| 148 | + # Get the length of the collection |
105 | 149 | length = len(collection) |
| 150 | + |
| 151 | + # Track whether any swaps occurred in this pass |
| 152 | + # If no swaps happen, the list is already sorted |
106 | 153 | swapped = False |
| 154 | + |
| 155 | + # Perform one complete pass: compare and swap adjacent elements |
107 | 156 | for i in range(length - 1): |
| 157 | + # Compare current element with the next element |
108 | 158 | if collection[i] > collection[i + 1]: |
| 159 | + # Swap them if they're in wrong order |
109 | 160 | collection[i], collection[i + 1] = collection[i + 1], collection[i] |
110 | 161 | swapped = True |
111 | 162 |
|
| 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 |
112 | 165 | return collection if not swapped else bubble_sort_recursive(collection) |
113 | 166 |
|
114 | 167 |
|
|
0 commit comments