Python Tutorial

Wednesday, April 24, 2013

Bubble sort in python

Bubble sort python code.

def bubble_sort(A):
    n = len(A)    
    for i in range(0,n):
        for j in range (i+1,n):
            if A[i]>A[j]:
                A[i],A[j]=A[j], A[i]

if __name__=="__main__":
    A = [7,3,5,2]
    print A
    bubble_sort(A)
    print A

Output:
[7, 3, 5, 2]
[2, 3, 5, 7]

0 comments:

Post a Comment