Created
October 20, 2014 18:21
-
-
Save pallabpain/635133a46d3d7e75e40d to your computer and use it in GitHub Desktop.
Bubble sort program in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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