# sort all the 0 and 1 in ascending order just by swapping adjecent elements a = "101101011001010100000101010011" s = list(a) l =len(s) n = 0 def findAndSwap(): t = 0 while(t < l-1): #print 's = ', s if s[t] > s[t+1]: _t = s[t] s[t] = s[t+1] s[t+1] = _t return True t = t+1 return False while True: if findAndSwap(): n = n+1 else: break print n
Yet another blog from yet another software engineer - a collection of my thoughts and some snippets of code I write (mainly for my later reference). If you find this useful, lets discuss in comments.
November 27, 2012
sorting binary digits
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment