bubble sort by eban

n=eval gets
a=$<.map &:to_i
j=s=0
n.times{|m|(n-m-1).times{|i|a[i,2]=a[i+1],a[i]if a[i]>a[i+1]&&s+=1
puts"compare = %2d   swap = %2d   data: #{["%2d"]*n*" "}"%[j+=1,s,*a]}}

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

download

return to the top page