1- from typing import Sequence , TypeVar
2-
1+ from typing import TypeVar
32
43T = TypeVar ("T" , int , float , str ) # comparable types
54
65
76def bubble_sort_iterative (collection : list [T ]) -> list [T ]:
8- """Pure implementation of bubble sort algorithm in Python
7+ """
8+ Pure implementation of bubble sort algorithm in Python.
99
10- :param collection: some mutable ordered collection with heterogeneous
11- comparable items inside
12- :return: the same collection ordered by ascending
10+ :param collection: list of comparable elements
11+ :return: the same collection ordered in ascending order
1312
1413 Examples:
1514 >>> bubble_sort_iterative([0, 5, 2, 3, 2])
@@ -20,47 +19,30 @@ def bubble_sort_iterative(collection: list[T]) -> list[T]:
2019 [-45, -5, -2]
2120 >>> bubble_sort_iterative([-23, 0, 6, -4, 34])
2221 [-23, -4, 0, 6, 34]
23- >>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
24- True
25- >>> bubble_sort_iterative([]) == sorted([])
26- True
27- >>> bubble_sort_iterative([-2, -45, -5]) == sorted([-2, -45, -5])
28- True
29- >>> bubble_sort_iterative([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
30- True
3122 >>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
3223 True
3324 >>> bubble_sort_iterative(['z', 'a', 'y', 'b', 'x', 'c'])
3425 ['a', 'b', 'c', 'x', 'y', 'z']
3526 >>> bubble_sort_iterative([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
3627 [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
37- >>> bubble_sort_iterative([1, 3.3, 5, 7.7, 2, 4.4, 6])
38- [1, 2, 3.3, 4.4, 5, 6, 7.7]
39- >>> import random
40- >>> collection_arg = random.sample(range(-50, 50), 100)
41- >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
42- True
43- >>> import string
44- >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
45- >>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
46- True
4728 """
4829 length = len (collection )
4930 for i in reversed (range (length )):
5031 swapped = False
5132 for j in range (i ):
5233 if collection [j ] > collection [j + 1 ]:
53- swapped = True
5434 collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
35+ swapped = True
5536 if not swapped :
56- break # Stop iteration if the collection is sorted.
37+ break
5738 return collection
5839
5940
6041def bubble_sort_recursive (collection : list [T ]) -> list [T ]:
61- """It is similar iterative bubble sort but recursive.
42+ """
43+ Recursive implementation of bubble sort.
6244
63- :param collection: mutable ordered sequence of elements
45+ :param collection: list of comparable elements
6446 :return: the same list in ascending order
6547
6648 Examples:
@@ -72,32 +54,12 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]:
7254 [-45, -5, -2]
7355 >>> bubble_sort_recursive([-23, 0, 6, -4, 34])
7456 [-23, -4, 0, 6, 34]
75- >>> bubble_sort_recursive([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
76- True
77- >>> bubble_sort_recursive([]) == sorted([])
78- True
79- >>> bubble_sort_recursive([-2, -45, -5]) == sorted([-2, -45, -5])
80- True
81- >>> bubble_sort_recursive([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
82- True
8357 >>> bubble_sort_recursive(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
8458 True
8559 >>> bubble_sort_recursive(['z', 'a', 'y', 'b', 'x', 'c'])
8660 ['a', 'b', 'c', 'x', 'y', 'z']
8761 >>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
8862 [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
89- >>> bubble_sort_recursive([1, 3.3, 5, 7.7, 2, 4.4, 6])
90- [1, 2, 3.3, 4.4, 5, 6, 7.7]
91- >>> bubble_sort_recursive(['a', 'Z', 'B', 'C', 'A', 'c'])
92- ['A', 'B', 'C', 'Z', 'a', 'c']
93- >>> import random
94- >>> collection_arg = random.sample(range(-50, 50), 100)
95- >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
96- True
97- >>> import string
98- >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
99- >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
100- True
10163 """
10264 length = len (collection )
10365 swapped = False
@@ -106,7 +68,10 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]:
10668 collection [i ], collection [i + 1 ] = collection [i + 1 ], collection [i ]
10769 swapped = True
10870
109- return collection if not swapped else bubble_sort_recursive (collection )
71+ if not swapped :
72+ return collection
73+ return bubble_sort_recursive (collection )
74+
11075
11176
11277if __name__ == "__main__" :
0 commit comments