Bubble Sort

this is not an optimized code(but my own implementation). if any faulty please do comment.

##bubble sort
## In bubble sort after every iteration the largest element bubbles ...
##... out to the right.


list1=[30,13,18,20,9,10,16,21]

#length of the string

length=len(list1) 

print list1

print "sorting begins \n ----------------"
##counter variable to count total number of swaps took place while sorting.
## not really necessary. 
cnt=0
for i in range(0,length):
    for j in range(0,length-1):
        if list1[j]>list1[j+1]:
            ## swapping process. 
            temp=list1[j+1]
            list1[j+1]=list1[j]
            list1[j]=temp
            cnt=cnt+1

        print list1
    print "--------"   

print "\n Sorted list", list1
print "total number of swaps ",cnt

        
Advertisements

One thought on “Bubble Sort

  1. [30, 13, 18, 20, 9, 10, 16, 21]
    sorting begins
    —————-
    [13, 30, 18, 20, 9, 10, 16, 21]
    [13, 18, 30, 20, 9, 10, 16, 21]
    [13, 18, 20, 30, 9, 10, 16, 21]
    [13, 18, 20, 9, 30, 10, 16, 21]
    [13, 18, 20, 9, 10, 30, 16, 21]
    [13, 18, 20, 9, 10, 16, 30, 21]
    [13, 18, 20, 9, 10, 16, 21, 30]
    ——–
    [13, 18, 20, 9, 10, 16, 21, 30]
    [13, 18, 20, 9, 10, 16, 21, 30]
    [13, 18, 9, 20, 10, 16, 21, 30]
    [13, 18, 9, 10, 20, 16, 21, 30]
    [13, 18, 9, 10, 16, 20, 21, 30]
    [13, 18, 9, 10, 16, 20, 21, 30]
    [13, 18, 9, 10, 16, 20, 21, 30]
    ——–
    [13, 18, 9, 10, 16, 20, 21, 30]
    [13, 9, 18, 10, 16, 20, 21, 30]
    [13, 9, 10, 18, 16, 20, 21, 30]
    [13, 9, 10, 16, 18, 20, 21, 30]
    [13, 9, 10, 16, 18, 20, 21, 30]
    [13, 9, 10, 16, 18, 20, 21, 30]
    [13, 9, 10, 16, 18, 20, 21, 30]
    ——–
    [9, 13, 10, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    ——–
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    ——–
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    ——–
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    ——–
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    [9, 10, 13, 16, 18, 20, 21, 30]
    ——–

    Sorted list [9, 10, 13, 16, 18, 20, 21, 30]
    total number of swaps 15
    >>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s