Skip to content

Instantly share code, notes, and snippets.

@pallabpain
Created October 20, 2014 18:21
Show Gist options
  • Save pallabpain/635133a46d3d7e75e40d to your computer and use it in GitHub Desktop.
Save pallabpain/635133a46d3d7e75e40d to your computer and use it in GitHub Desktop.
Bubble sort program in Python
def swap(p,q):
return q,p
a=[]
x=int(input("Enter the number of elements you want to sort : "))
i=0
while (i < x):
y=int(input("Enter element " + str(i+1) + " : "))
a.append(y)
i+=1
print (a)
i=1
j=0
while (i<x):
j = 0
k = (x-i-1)
while (j < x-i):
if (a[j] > a[j+1] or a[k] < a[k-1] ):
a[j],a[j+1]=swap(a[j],a[j+1])
j+=1
k-=1
i+=1
print (a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment