Skip to content

Commit ef44a18

Browse files
committed
Consolidate bubble sort iterative and recursive
1 parent 72bd653 commit ef44a18

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

Diff for: sorts/bubble_sort.py

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
from typing import Any
22

33

4-
def bubble_sort(collection: list[Any]) -> list[Any]:
4+
def bubble_sort_iterative(collection: list[Any]) -> list[Any]:
55
"""Pure implementation of bubble sort algorithm in Python
66
77
:param collection: some mutable ordered collection with heterogeneous
88
comparable items inside
99
:return: the same collection ordered by ascending
1010
1111
Examples:
12-
>>> bubble_sort([0, 5, 2, 3, 2])
12+
>>> bubble_sort_iterative([0, 5, 2, 3, 2])
1313
[0, 2, 2, 3, 5]
14-
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
14+
>>> bubble_sort_iterative([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
1515
True
16-
>>> bubble_sort([]) == sorted([])
16+
>>> bubble_sort_iterative([]) == sorted([])
1717
True
18-
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
18+
>>> bubble_sort_iterative([-2, -45, -5]) == sorted([-2, -45, -5])
1919
True
20-
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
20+
>>> bubble_sort_iterative([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
2121
True
22-
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
22+
>>> bubble_sort_iterative(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
2323
True
2424
>>> import random
2525
>>> collection = random.sample(range(-50, 50), 100)
26-
>>> bubble_sort(collection) == sorted(collection)
26+
>>> bubble_sort_iterative(collection) == sorted(collection)
2727
True
2828
>>> import string
2929
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
30-
>>> bubble_sort(collection) == sorted(collection)
30+
>>> bubble_sort_iterative(collection) == sorted(collection)
3131
True
3232
"""
3333
length = len(collection)
@@ -42,6 +42,44 @@ def bubble_sort(collection: list[Any]) -> list[Any]:
4242
return collection
4343

4444

45+
def bubble_sort_recursive(list_data: list, length: int = 0) -> list:
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
50+
:return: the same list in ascending order
51+
52+
>>> bubble_sort_recursive([0, 5, 2, 3, 2], 5)
53+
[0, 2, 2, 3, 5]
54+
55+
>>> bubble_sort_recursive([], 0)
56+
[]
57+
58+
>>> bubble_sort_recursive([-2, -45, -5], 3)
59+
[-45, -5, -2]
60+
61+
>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5)
62+
[-23, -4, 0, 6, 34]
63+
64+
>>> bubble_sort_recursive([-23, 0, 6, -4, 34], 5) == sorted([-23, 0, 6, -4, 34])
65+
True
66+
67+
>>> bubble_sort_recursive(['z','a','y','b','x','c'], 6)
68+
['a', 'b', 'c', 'x', 'y', 'z']
69+
70+
>>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
71+
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
72+
"""
73+
length = length or len(list_data)
74+
swapped = False
75+
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]
78+
swapped = True
79+
80+
return list_data if not swapped else bubble_sort_recursive(list_data, length - 1)
81+
82+
4583
if __name__ == "__main__":
4684
import doctest
4785
import time
@@ -50,6 +88,13 @@ def bubble_sort(collection: list[Any]) -> list[Any]:
5088

5189
user_input = input("Enter numbers separated by a comma:").strip()
5290
unsorted = [int(item) for item in user_input.split(",")]
91+
92+
print("Iterative bubble sort:")
93+
start = time.process_time()
94+
print(*bubble_sort_iterative(unsorted), sep=",")
95+
print(f"Processing time (iterative): {(time.process_time() - start) % 1e9 + 7}")
96+
97+
print("\nRecursive bubble sort:")
5398
start = time.process_time()
54-
print(*bubble_sort(unsorted), sep=",")
55-
print(f"Processing time: {(time.process_time() - start)%1e9 + 7}")
99+
print(bubble_sort_recursive(unsorted, len(unsorted)))
100+
print(f"Processing time (recursive): {(time.process_time() - start) % 1e9 + 7}")

Diff for: sorts/recursive_bubble_sort.py

-42
This file was deleted.

0 commit comments

Comments
 (0)