Skip to content

Commit d4fc55c

Browse files
hkp27299cclauss
authored andcommitted
Add files via upload (TheAlgorithms#1657)
1 parent 1d606d8 commit d4fc55c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

sorts/recursive_bubble_sort.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def bubble_sort(list1):
2+
"""
3+
It is similar is bubble sort but recursive.
4+
:param list1: mutable ordered sequence of elements
5+
:return: the same list in ascending order
6+
7+
>>> bubble_sort([0, 5, 2, 3, 2])
8+
[0, 2, 2, 3, 5]
9+
10+
>>> bubble_sort([])
11+
[]
12+
13+
>>> bubble_sort([-2, -45, -5])
14+
[-45, -5, -2]
15+
16+
>>> bubble_sort([-23, 0, 6, -4, 34])
17+
[-23, -4, 0, 6, 34]
18+
19+
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
20+
True
21+
22+
>>> bubble_sort(['z','a','y','b','x','c'])
23+
['a', 'b', 'c', 'x', 'y', 'z']
24+
25+
"""
26+
27+
for i, num in enumerate(list1):
28+
try:
29+
if list1[i+1] < num:
30+
list1[i] = list1[i+1]
31+
list1[i+1] = num
32+
bubble_sort(list1)
33+
except IndexError:
34+
pass
35+
return list1
36+
37+
if __name__ == "__main__":
38+
list1 = [33,99,22,11,66]
39+
bubble_sort(list1)
40+
print(list1)

0 commit comments

Comments
 (0)