Skip to content

Commit e7db920

Browse files
authored
Bubble_Sort
Hello this is my first time participating this year. I have added Bubble sort code. I would be obliged if you could accept my pull request
1 parent 3c49e64 commit e7db920

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Bubble_Sort.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Python Program for implementation of
2+
# Recursive Bubble sort
3+
class bubbleSort:
4+
5+
def __init__(self, array):
6+
self.array = array
7+
self.length = len(array)
8+
9+
def __str__(self):
10+
return " ".join([str(x)
11+
for x in self.array])
12+
13+
def bubbleSortRecursive(self, n=None):
14+
if n is None:
15+
n = self.length
16+
count = 0
17+
18+
# Base case
19+
if n == 1:
20+
return
21+
# One pass of bubble sort. After
22+
# this pass, the largest element
23+
# is moved (or bubbled) to end.
24+
for i in range(n - 1):
25+
if self.array[i] > self.array[i + 1]:
26+
self.array[i], self.array[i +
27+
1] = self.array[i + 1], self.array[i]
28+
count = count + 1
29+
30+
# Check if any recursion happens or not
31+
# If any recursion is not happen then return
32+
if (count==0):
33+
return
34+
35+
# Largest element is fixed,
36+
# recur for remaining array
37+
self.bubbleSortRecursive(n - 1)
38+
39+
# Driver Code
40+
def main():
41+
array = [64, 34, 25, 12, 22, 11, 90]
42+
43+
sort = bubbleSort(array)
44+
45+
sort.bubbleSortRecursive()
46+
print("Sorted array :\n", sort)
47+
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)