bubble sort by pooq

b=(a=scan("stdin"))[1]
n=a[-1]
c=s=0
for(i in 1:(b-1))for(j in 1:(b-i)){c=c+1;t=n[j+1];if(t<n[j]){s=s+1;n[j+1]=n[j];n[j]=t};
cat(sprintf("compare =%3d   swap =%3d   data:",c,s));for(k in 1:b)cat(sprintf("%3d",n[k]));cat('
')}

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

download

return to the top page