Bubble-sort in python
Bubble sort is a sorting algo that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
It is a popular, but inefficient sorting algo, it has worst-case and average complexity both О(n2), where n is the number of items being sorted.
Here is an implementation in python
count = len(B)
for i in xrange(count):
for j in xrange(count-1,i,-1):
#print "compairing %i with %i"%(B[j],B[j-1])
if B[j] < B[j - 1]:
temp = B[j]
B[j] = B[j - 1]
B[j - 1] = temp
#print "Swapping %i with %i"%(B[j],B[j-1])
#print "Transformed Array: " , B
B = [6,5,3,1,8,7,2,4]
print "Unsorted: ", B
bubbleSort(B)
print "Bubble Sorted: ", B
Hope this helps
Read other posts