bubble sort by recursive

n=input();A=[input()for i in" "*n]
c=s=0
while n:
 n-=1
 for j in range(n):f=A[j]>A[j+1];A[j:j+2]=A[j+f],A[j+1-f];s+=f;c+=1;print"compare =%3s   swap =%3s   data:"%(c,s)+"%3s"*len(A)%tuple(A)

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

download

return to the top page