1- from typing import Any
1+ from typing import Protocol , TypeVar
22
33
4- def bubble_sort_iterative (collection : list [Any ]) -> list [Any ]:
4+ class Comparable (Protocol ):
5+ def __lt__ (self , other : object , / ) -> bool : ...
6+
7+
8+ T = TypeVar ("T" , bound = Comparable )
9+
10+
11+ def bubble_sort_iterative (collection : list [T ]) -> list [T ]:
512 """Pure implementation of the bubble sort algorithm in Python (iterative).
613
714 Bubble sort works by repeatedly stepping through the collection,
@@ -50,6 +57,10 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5057 [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
5158 >>> bubble_sort_iterative([1, 3.3, 5, 7.7, 2, 4.4, 6])
5259 [1, 2, 3.3, 4.4, 5, 6, 7.7]
60+ >>> bubble_sort_iterative([1, "a"])
61+ Traceback (most recent call last):
62+ ...
63+ TypeError: '<' not supported between instances of 'str' and 'int'
5364 >>> import random
5465 >>> collection_arg = random.sample(range(-50, 50), 100)
5566 >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
@@ -63,15 +74,15 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
6374 for i in reversed (range (length )):
6475 swapped = False
6576 for j in range (i ):
66- if collection [j ] > collection [j + 1 ]:
77+ if collection [j + 1 ] < collection [j ]:
6778 swapped = True
6879 collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
6980 if not swapped :
7081 break # Stop iteration if the collection is sorted.
7182 return collection
7283
7384
74- def bubble_sort_recursive (collection : list [Any ]) -> list [Any ]:
85+ def bubble_sort_recursive (collection : list [T ]) -> list [T ]:
7586 """Pure implementation of the bubble sort algorithm in Python (recursive).
7687
7788 Functionally identical to the iterative version: each call makes a
@@ -114,6 +125,10 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
114125 [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
115126 >>> bubble_sort_recursive([1, 3.3, 5, 7.7, 2, 4.4, 6])
116127 [1, 2, 3.3, 4.4, 5, 6, 7.7]
128+ >>> bubble_sort_recursive([1, "a"])
129+ Traceback (most recent call last):
130+ ...
131+ TypeError: '<' not supported between instances of 'str' and 'int'
117132 >>> bubble_sort_recursive(['a', 'Z', 'B', 'C', 'A', 'c'])
118133 ['A', 'B', 'C', 'Z', 'a', 'c']
119134 >>> import random
@@ -128,7 +143,7 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
128143 length = len (collection )
129144 swapped = False
130145 for i in range (length - 1 ):
131- if collection [i ] > collection [i + 1 ]:
146+ if collection [i + 1 ] < collection [i ]:
132147 collection [i ], collection [i + 1 ] = collection [i + 1 ], collection [i ]
133148 swapped = True
134149
0 commit comments