MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / bubble_sort_recursive

Function bubble_sort_recursive

sorts/bubble_sort.py:63–112  ·  view source on GitHub ↗

It is similar iterative bubble sort but recursive. :param collection: mutable ordered sequence of elements :return: the same list in ascending order Examples: >>> bubble_sort_recursive([0, 5, 2, 3, 2]) [0, 2, 2, 3, 5] >>> bubble_sort_recursive([]) [] >>> bubble_sort

(collection: list[Any])

Source from the content-addressed store, hash-verified

61
62
63def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
64 """It is similar iterative bubble sort but recursive.
65
66 :param collection: mutable ordered sequence of elements
67 :return: the same list in ascending order
68
69 Examples:
70 >>> bubble_sort_recursive([0, 5, 2, 3, 2])
71 [0, 2, 2, 3, 5]
72 >>> bubble_sort_recursive([])
73 []
74 >>> bubble_sort_recursive([-2, -45, -5])
75 [-45, -5, -2]
76 >>> bubble_sort_recursive([-23, 0, 6, -4, 34])
77 [-23, -4, 0, 6, 34]
78 >>> bubble_sort_recursive([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
79 True
80 >>> bubble_sort_recursive([]) == sorted([])
81 True
82 >>> bubble_sort_recursive([-2, -45, -5]) == sorted([-2, -45, -5])
83 True
84 >>> bubble_sort_recursive([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
85 True
86 >>> bubble_sort_recursive(['d', 'a', 'b', 'e']) == sorted(['d', 'a', 'b', 'e'])
87 True
88 >>> bubble_sort_recursive(['z', 'a', 'y', 'b', 'x', 'c'])
89 ['a', 'b', 'c', 'x', 'y', 'z']
90 >>> bubble_sort_recursive([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6])
91 [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
92 >>> bubble_sort_recursive([1, 3.3, 5, 7.7, 2, 4.4, 6])
93 [1, 2, 3.3, 4.4, 5, 6, 7.7]
94 >>> bubble_sort_recursive(['a', 'Z', 'B', 'C', 'A', 'c'])
95 ['A', 'B', 'C', 'Z', 'a', 'c']
96 >>> import random
97 >>> collection_arg = random.sample(range(-50, 50), 100)
98 >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
99 True
100 >>> import string
101 >>> collection_arg = random.choices(string.ascii_letters + string.digits, k=100)
102 >>> bubble_sort_recursive(collection_arg) == sorted(collection_arg)
103 True
104 """
105 length = len(collection)
106 swapped = False
107 for i in range(length - 1):
108 if collection[i] > collection[i + 1]:
109 collection[i], collection[i + 1] = collection[i + 1], collection[i]
110 swapped = True
111
112 return collection if not swapped else bubble_sort_recursive(collection)
113
114
115if __name__ == "__main__":

Callers 1

bubble_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected