1
- from typing import Sequence , TypeVar
2
-
1
+ from typing import TypeVar
3
2
4
3
T = TypeVar ("T" , int , float , str ) # comparable types
5
4
6
5
7
6
def 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.
9
9
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
13
12
14
13
Examples:
15
14
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
@@ -20,47 +19,30 @@ def bubble_sort_iterative(collection: list[T]) -> list[T]:
20
19
[-45, -5, -2]
21
20
>>> bubble_sort_iterative([-23, 0, 6, -4, 34])
22
21
[-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
31
22
>>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
32
23
True
33
24
>>> bubble_sort_iterative(['z', 'a', 'y', 'b', 'x', 'c'])
34
25
['a', 'b', 'c', 'x', 'y', 'z']
35
26
>>> bubble_sort_iterative([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
36
27
[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
47
28
"""
48
29
length = len (collection )
49
30
for i in reversed (range (length )):
50
31
swapped = False
51
32
for j in range (i ):
52
33
if collection [j ] > collection [j + 1 ]:
53
- swapped = True
54
34
collection [j ], collection [j + 1 ] = collection [j + 1 ], collection [j ]
35
+ swapped = True
55
36
if not swapped :
56
- break # Stop iteration if the collection is sorted.
37
+ break
57
38
return collection
58
39
59
40
60
41
def bubble_sort_recursive (collection : list [T ]) -> list [T ]:
61
- """It is similar iterative bubble sort but recursive.
42
+ """
43
+ Recursive implementation of bubble sort.
62
44
63
- :param collection: mutable ordered sequence of elements
45
+ :param collection: list of comparable elements
64
46
:return: the same list in ascending order
65
47
66
48
Examples:
@@ -72,32 +54,12 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]:
72
54
[-45, -5, -2]
73
55
>>> bubble_sort_recursive([-23, 0, 6, -4, 34])
74
56
[-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
83
57
>>> bubble_sort_recursive(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
84
58
True
85
59
>>> bubble_sort_recursive(['z', 'a', 'y', 'b', 'x', 'c'])
86
60
['a', 'b', 'c', 'x', 'y', 'z']
87
61
>>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
88
62
[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
101
63
"""
102
64
length = len (collection )
103
65
swapped = False
@@ -106,7 +68,10 @@ def bubble_sort_recursive(collection: list[T]) -> list[T]:
106
68
collection [i ], collection [i + 1 ] = collection [i + 1 ], collection [i ]
107
69
swapped = True
108
70
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
+
110
75
111
76
112
77
if __name__ == "__main__" :
0 commit comments