Showing Page:
1/1
Program for Bubble Sort in Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Arr = [21,14,18,25,9]
n = len(Arr) #length
Pass = 1
while(Pass<=n-1):
index = 0
while(index<=n-Pass-1):
if Arr[index]>Arr[index+1]:
Arr[index],Arr[index+1] = Arr[index+1],Arr[index] #swapping
index = index+1
Pass =Pass + 1
for item in Arr:
print item,
Output
9 14 18 21 25

Unformatted Attachment Preview

Program for Bubble Sort in Python 1 Arr = [21,14,18,25,9] 2 n = len(Arr) #length 3 4 Pass = 1 5 6 while(Pass<=n-1): 7 index = 0 8 while(index<=n-Pass-1): 9 10 if Arr[index]>Arr[index+1]: Arr[index],Arr[index+1] = Arr[index+1],Arr[index] 11 12 index = index+1 13 14 Pass =Pass + 1 15 16 17 for item in Arr: print item, Output 9 14 18 21 25 #swapping Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4