forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBubble sort
More file actions
29 lines (24 loc) · 696 Bytes
/
Bubble sort
File metadata and controls
29 lines (24 loc) · 696 Bytes
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
#https://www.facebook.com/permalink.php?story_fbid=103039851600105&id=100056822710327
#Subscribed by Sonali Gupta
import math
import os
import random
import re
import sys
n = int(input())
a = list(map(int, input().rstrip().split()))
def bubblesort(a,n):
numofswaps = 0
for i in range (n):
for j in range (n-1-i):
if a[j] > a[j + 1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
numofswaps += 1
if numofswaps == 0:
break
print("Array is sorted in " + str(numofswaps) +" swaps.")
print("First Element: "+str(a[0]))
print("Last Element: "+str(a[len(a)-1]))
bubblesort(a,n)