Skip to content

Commit b9013d1

Browse files
I have changed some code in sorts/bubble_sort.py
1 parent e9e7c96 commit b9013d1

File tree

1 file changed

+26
-109
lines changed

1 file changed

+26
-109
lines changed

sorts/bubble_sort.py

+26-109
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,15 @@
1-
from typing import Any
1+
from typing import Any, List
22

3+
def bubble_sort_iterative(collection: List[Any]) -> List[Any]:
4+
"""Sorts a collection using the iterative bubble sort algorithm.
35
4-
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5-
"""Pure implementation of bubble sort algorithm in Python
6-
7-
:param collection: some mutable ordered collection with heterogeneous
8-
comparable items inside
9-
:return: the same collection ordered by ascending
10-
11-
Examples:
12-
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
13-
[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]
20-
>>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
21-
True
22-
>>> bubble_sort_iterative([]) == sorted([])
23-
True
24-
>>> bubble_sort_iterative([-2, -45, -5]) == sorted([-2, -45, -5])
25-
True
26-
>>> bubble_sort_iterative([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
27-
True
28-
>>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
29-
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]
36-
>>> import random
37-
>>> collection_arg = random.sample(range(-50, 50), 100)
38-
>>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
39-
True
40-
>>> import string
41-
>>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
42-
>>> bubble_sort_iterative(collection_arg) == sorted(collection_arg)
43-
True
6+
:param collection: A mutable ordered collection with comparable items.
7+
:return: The same collection ordered in ascending order.
448
"""
459
length = len(collection)
46-
for i in reversed(range(length)):
10+
for i in range(length):
4711
swapped = False
48-
for j in range(i):
12+
for j in range(length - 1 - i):
4913
if collection[j] > collection[j + 1]:
5014
swapped = True
5115
collection[j], collection[j + 1] = collection[j + 1], collection[j]
@@ -54,77 +18,30 @@ def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
5418
return collection
5519

5620

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
61-
:return: the same list in ascending order
21+
def bubble_sort_recursive(collection: List[Any]) -> List[Any]:
22+
"""Sorts a collection using the recursive bubble sort algorithm.
6223
63-
Examples:
64-
>>> bubble_sort_recursive([0, 5, 2, 3, 2])
65-
[0, 2, 2, 3, 5]
66-
>>> bubble_sort_iterative([])
67-
[]
68-
>>> bubble_sort_recursive([-2, -45, -5])
69-
[-45, -5, -2]
70-
>>> bubble_sort_recursive([-23, 0, 6, -4, 34])
71-
[-23, -4, 0, 6, 34]
72-
>>> bubble_sort_recursive([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
73-
True
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'])
83-
['a', 'b', 'c', 'x', 'y', 'z']
84-
>>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
85-
[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
24+
:param collection: A mutable ordered sequence of elements.
25+
:return: The same list in ascending order.
9626
"""
97-
length = len(collection)
27+
if len(collection) <= 1:
28+
return collection
29+
30+
# Perform a single pass of bubble sort
9831
swapped = False
99-
for i in range(length - 1):
32+
for i in range(len(collection) - 1):
10033
if collection[i] > collection[i + 1]:
101-
collection[i], collection[i + 1] = collection[i + 1], collection[i]
10234
swapped = True
103-
104-
return collection if not swapped else bubble_sort_recursive(collection)
35+
collection[i], collection[i + 1] = collection[i + 1], collection[i]
36+
37+
if not swapped:
38+
return collection
39+
40+
return bubble_sort_recursive(collection[:-1]) + [collection[-1]]
10541

10642

10743
if __name__ == "__main__":
108-
import doctest
109-
from random import sample
110-
from timeit import timeit
111-
112-
doctest.testmod()
113-
114-
# Benchmark: Iterative seems slightly faster than recursive.
115-
num_runs = 10_000
116-
unsorted = sample(range(-50, 50), 100)
117-
timer_iterative = timeit(
118-
"bubble_sort_iterative(unsorted[:])", globals=globals(), number=num_runs
119-
)
120-
print("\nIterative bubble sort:")
121-
print(*bubble_sort_iterative(unsorted), sep=",")
122-
print(f"Processing time (iterative): {timer_iterative:.5f}s for {num_runs:,} runs")
123-
124-
unsorted = sample(range(-50, 50), 100)
125-
timer_recursive = timeit(
126-
"bubble_sort_recursive(unsorted[:])", globals=globals(), number=num_runs
127-
)
128-
print("\nRecursive bubble sort:")
129-
print(*bubble_sort_recursive(unsorted), sep=",")
130-
print(f"Processing time (recursive): {timer_recursive:.5f}s for {num_runs:,} runs")
44+
# Example usage
45+
sample_list = [5, 3, 8, 6, 2]
46+
print("Iterative Bubble Sort:", bubble_sort_iterative(sample_list.copy()))
47+
print("Recursive Bubble Sort:", bubble_sort_recursive(sample_list.copy()))

0 commit comments

Comments
 (0)