Skip to content

Commit c7ea86e

Browse files
committed
Refactor bubble sort func signature, doctest, timer
1 parent 1dacbec commit c7ea86e

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

Diff for: sorts/bubble_sort.py

+65-34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
1111
Examples:
1212
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
1313
[0, 2, 2, 3, 5]
14+
>>> bubble_sort_iterative([])
15+
[]
16+
>>> bubble_sort_iterative([-2, -45, -5])
17+
[-45, -5, -2]
18+
>>> bubble_sort_iterative([-23, 0, 6, -4, 34])
19+
[-23, -4, 0, 6, 34]
1420
>>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
1521
True
1622
>>> bubble_sort_iterative([]) == sorted([])
@@ -21,13 +27,19 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
2127
True
2228
>>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
2329
True
30+
>>> bubble_sort_iterative(['z', 'a', 'y', 'b', 'x', 'c'])
31+
['a', 'b', 'c', 'x', 'y', 'z']
32+
>>> bubble_sort_iterative([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
33+
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
34+
>>> bubble_sort_iterative([1, 3.3, 5, 7.7, 2, 4.4, 6])
35+
[1, 2, 3.3, 4.4, 5, 6, 7.7]
2436
>>> import random
25-
>>> collection = random.sample(range(-50, 50), 100)
26-
>>> bubble_sort_iterative(collection) == sorted(collection)
37+
>>> collection_arg = random.sample(range(-50, 50), 100)
38+
>>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
2739
True
2840
>>> import string
29-
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
30-
>>> bubble_sort_iterative(collection) == sorted(collection)
41+
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
42+
>>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
3143
True
3244
"""
3345
length = len(collection)
@@ -42,59 +54,78 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
4254
return collection
4355

4456

45-
def bubble_sort_recursive(collection: list[Any]), length: int = 0) -> list[Any]:
46-
"""
47-
It is similar is bubble sort but recursive.
48-
:param list_data: mutable ordered sequence of elements
49-
:param length: length of list data
57+
def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
58+
"""It is similar iterative bubble sort but recursive.
59+
60+
:param collection: mutable ordered sequence of elements
5061
:return: the same list in ascending order
5162
52-
>>> bubble_sort_recursive([0, 5, 2, 3, 2], 5)
63+
Examples:
64+
>>> bubble_sort_recursive([0, 5, 2, 3, 2])
5365
[0, 2, 2, 3, 5]
54-
55-
>>> bubble_sort_recursive([], 0)
66+
>>> bubble_sort_iterative([])
5667
[]
57-
58-
>>> bubble_sort_recursive([-2, -45, -5], 3)
68+
>>> bubble_sort_recursive([-2, -45, -5])
5969
[-45, -5, -2]
60-
61-
>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5)
70+
>>> bubble_sort_recursive([-23, 0, 6, -4, 34])
6271
[-23, -4, 0, 6, 34]
63-
64-
>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5) == sorted([-23, 0, 6, -4, 34])
72+
>>> bubble_sort_recursive([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
6573
True
66-
67-
>>> bubble_sort_recursive(['z','a','y','b','x','c'], 6)
74+
>>> bubble_sort_recursive([]) == sorted([])
75+
True
76+
>>> bubble_sort_recursive([-2, -45, -5]) == sorted([-2, -45, -5])
77+
True
78+
>>> bubble_sort_recursive([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
79+
True
80+
>>> bubble_sort_recursive(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
81+
True
82+
>>> bubble_sort_recursive(['z', 'a', 'y', 'b', 'x', 'c'])
6883
['a', 'b', 'c', 'x', 'y', 'z']
69-
7084
>>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
7185
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
86+
>>> bubble_sort_recursive([1, 3.3, 5, 7.7, 2, 4.4, 6])
87+
[1, 2, 3.3, 4.4, 5, 6, 7.7]
88+
>>> import random
89+
>>> collection_arg = random.sample(range(-50, 50), 100)
90+
>>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
91+
True
92+
>>> import string
93+
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
94+
>>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
95+
True
7296
"""
73-
length = length or len(list_data)
97+
length = len(collection)
7498
swapped = False
7599
for i in range(length - 1):
76-
if list_data[i] > list_data[i + 1]:
77-
list_data[i], list_data[i + 1] = list_data[i + 1], list_data[i]
100+
if collection[i] > collection[i + 1]:
101+
collection[i], collection[i + 1] = collection[i + 1], collection[i]
78102
swapped = True
79103

80-
return list_data if not swapped else bubble_sort_recursive(list_data, length - 1)
104+
return collection if not swapped else bubble_sort_recursive(collection)
81105

82106

83107
if __name__ == "__main__":
84108
import doctest
85-
import time
109+
from timeit import timeit
86110

87111
doctest.testmod()
88112

89113
user_input = input("Enter numbers separated by a comma:").strip()
90-
unsorted = [int(item) for item in user_input.split(",")]
91-
92-
print("Iterative bubble sort:")
93-
start = time.process_time()
114+
unsorted = [int(item) if isinstance(item, int) else float(item)
115+
for item in user_input.split(",")]
116+
117+
num_runs = 1000
118+
timer_iterative = timeit('bubble_sort_iterative(unsorted[:])',
119+
globals=globals(),
120+
number=num_runs)
121+
timer_recursive = timeit('bubble_sort_recursive(unsorted[:])',
122+
globals=globals(),
123+
number=num_runs)
124+
125+
print("\nIterative bubble sort:")
94126
print(*bubble_sort_iterative(unsorted), sep=",")
95-
print(f"Processing time (iterative): {(time.process_time() - start) % 1e9 + 7}")
127+
print(f"Processing time (iterative): {timer_iterative:.5f} s for {num_runs} runs")
96128

97129
print("\nRecursive bubble sort:")
98-
start = time.process_time()
99-
print(bubble_sort_recursive(unsorted, len(unsorted)))
100-
print(f"Processing time (recursive): {(time.process_time() - start) % 1e9 + 7}")
130+
print(*bubble_sort_recursive(unsorted), sep=",")
131+
print(f"Processing time (recursive): {timer_recursive:.5f} s for {num_runs} runs")

0 commit comments

Comments
 (0)