bubble sort by rolf

R=range
n=input()-1
s=[input()for i in R(n+1)]
c=d=0
for i in R(n):
 for j in R(n-i):
  if s[j]>s[j+1]:s[j],s[j+1]=s[j+1],s[j];c+=1
  d+=1
  print"compare = %2d   swap = %2d   data: %s"%(d,c," ".join(map(lambda x:"%2d"%x,s)))

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page