1
1
from typing import Any
2
2
3
3
4
- def bubble_sort (collection : list [Any ]) -> list [Any ]:
4
+ def bubble_sort_iterative (collection : list [Any ]) -> list [Any ]:
5
5
"""Pure implementation of bubble sort algorithm in Python
6
6
7
7
:param collection: some mutable ordered collection with heterogeneous
8
8
comparable items inside
9
9
:return: the same collection ordered by ascending
10
10
11
11
Examples:
12
- >>> bubble_sort ([0, 5, 2, 3, 2])
12
+ >>> bubble_sort_iterative ([0, 5, 2, 3, 2])
13
13
[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])
15
15
True
16
- >>> bubble_sort ([]) == sorted([])
16
+ >>> bubble_sort_iterative ([]) == sorted([])
17
17
True
18
- >>> bubble_sort ([-2, -45, -5]) == sorted([-2, -45, -5])
18
+ >>> bubble_sort_iterative ([-2, -45, -5]) == sorted([-2, -45, -5])
19
19
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])
21
21
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'])
23
23
True
24
24
>>> import random
25
25
>>> collection = random.sample(range(-50, 50), 100)
26
- >>> bubble_sort (collection) == sorted(collection)
26
+ >>> bubble_sort_iterative (collection) == sorted(collection)
27
27
True
28
28
>>> import string
29
29
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
30
- >>> bubble_sort (collection) == sorted(collection)
30
+ >>> bubble_sort_iterative (collection) == sorted(collection)
31
31
True
32
32
"""
33
33
length = len (collection )
@@ -42,6 +42,44 @@ def bubble_sort(collection: list[Any]) -> list[Any]:
42
42
return collection
43
43
44
44
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
+
45
83
if __name__ == "__main__" :
46
84
import doctest
47
85
import time
@@ -50,6 +88,13 @@ def bubble_sort(collection: list[Any]) -> list[Any]:
50
88
51
89
user_input = input ("Enter numbers separated by a comma:" ).strip ()
52
90
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 ("\n Recursive bubble sort:" )
53
98
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 } " )
0 commit comments