bubble sort by nn

d="$System.in".replaceAll(/\b.\b/,' $0').tokenize('\n')
n=d.size()-2
for(d-=d[c=s=0];n--;)0.upto(n){i->s+=v=(u=d[i])>(d[i]=d[i+1])?1:0
d[i+v]=u
printf('compare = %2d   swap = %2d   data: %s\n',++c,s,d.join(' '))}

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

download

return to the top page