forked from souravjain540/Basic-Python-Programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort.py
More file actions
Latest commit
50 lines (39 loc) · 1.3 KB
/
Copy pathBubble_Sort.py
File metadata and controls
50 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Python Program for implementation of
# Recursive Bubble sort
classbubbleSort:
def__init__(self, array):
self.array=array
self.length=len(array)
def__str__(self):
return" ".join([str(x)
forxinself.array])
defbubbleSortRecursive(self, n=None):
ifnisNone:
n=self.length
count=0
# Base case
ifn==1:
return
# One pass of bubble sort. After
# this pass, the largest element
# is moved (or bubbled) to end.
foriinrange(n-1):
ifself.array[i] >self.array[i+1]:
self.array[i], self.array[i+
1] =self.array[i+1], self.array[i]
count=count+1
# Check if any recursion happens or not
# If any recursion is not happen then return
if (count==0):
return
# Largest element is fixed,
# recur for remaining array
self.bubbleSortRecursive(n-1)
# Driver Code
defmain():
array= [64, 34, 25, 12, 22, 11, 90]
sort=bubbleSort(array)
sort.bubbleSortRecursive()
print("Sorted array :\n", sort)
if__name__=="__main__":
main()