bubble sort by mokos

n,*a=$<.map{|s|"%3s"%s.chop}
s=c=0
2.upto(n=n.to_i){|i|0.upto(n-i){|j|x,y=a[j,2]
x>y&&(a[j,2]=y,x;s+=1)
puts"compare =%3d   swap =%3d   data:#{a}"%[c+=1,s]}}

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

download

return to the top page