- Author:
- Petar
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/552
- Shortlink:
- https://js1k.com/552
- Blog post:
- please update here!
- Bytes:
- 892
- Chars:
- 892
- Submission
window.onload=s;var o=document.getElementById("c"),c=o.getContext("2d"),a=[],i=-1,h,t,b=0,j=-1,z=i,u=800,m=5,n=10,y="#0067A5",k="#483C32",f="0px Arial";function s(){o.width=u;o.height=u;while(++i<80)a[i]=i;i=z;while(++i<80)w(r(),r());t=setInterval(d,0);}function r(){return Math.round(Math.random()*79)}function w(e,q){h=a[q];a[q]=a[e];a[e]=h;}function d(){if(++j>78){j=0;b++;}if(b>80)clearInterval(t);if(a[j]<a[j+1]){w(j,j+1);c.fillStyle="#8C92AC";c.fillRect(0,0,u,u);g(1);g(2);c.fillStyle="#480607";c.font=4+f;c.fillText("BUBBLE SORT",262,390);c.font=2+f;c.fillText("x=position, y=value",315,430);c.fillText("x",390,790);c.fillText("y",780,390);c.fillStyle="#00CED1";i=z;while(++i<80)c.fillRect(i*n,a[i]*n,n,n);}}function g(l){for(i=1,x=n;x<u;++i,x+=n){c.beginPath();c.strokeStyle=(i%m)?y:k;if(l==1){c.moveTo(x,0);c.lineTo(x,u);}else{c.moveTo(0,x);c.lineTo(u,x);}c.stroke();c.closePath();}}
- Description
- Graphic representation of bubble sort
x-axis represents position in the array and y-axis represents the value at that position
array contains numbers from 0 to 80 randomly disseminated
and then sort descending
- Base64 encoded
d2luZG93Lm9ubG9hZD1zO3ZhciBvPWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCJjIiksYz1vLmdldENvbnRleHQoIjJkIiksYT1bXSxpPS0xLGgsdCxiPTAsaj0tMSx6PWksdT04MDAsbT01LG49MTAseT0iIzAwNjdBNSIsaz0iIzQ4M0MzMiIsZj0iMHB4IEFyaWFsIjtmdW5jdGlvbiBzKCl7by53aWR0aD11O28uaGVpZ2h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
f="0px Arial";
function s()//set canvas width, height init array, ranom swap
{
o.width=u;
o.height=u;
while(++i<80)
a[i]=i;i=z;
while(++i<80)
w(r(),r());
t=setInterval(d,0);
}
function r()//generate random number for random swap
{
return Math.round(Math.random()*79)
}
function w(e,q)// input two index of array and swap values
{
h=a[q];
a[q]=a[e];
a[e]=h;
}
function d()//sort array and draw background, text and call grid draw
{
if(++j>78)
{
j=0;
b++;
}
if(b>80)
clearInterval(t);
if(a[j]<a[j+1])
{
w(j,j+1);
c.fillStyle="#8C92AC";
c.fillRect(0,0,u,u);
g(1);
g(2);
c.fillStyle="#480607";
c.font=4+f;
c.fillText("BUBBLE SORT",262,390);
c.font=2+f;
c.fillText("x=position, y=value",315,430);
c.fillText("x",390,790);
c.fillText("y",780,390);
c.fillStyle="#00CED1";
i=z;
while(++i<80)
c.fillRect(i*n,a[i]*n,n,n);
}
}
function g(l)//draw grid
{
for(i=1,x=n;x<u;++i,x+=n)
{
c.beginPath();
c.strokeStyle=(i%m)?y:k;
if(l==1)
{
c.moveTo(x,0);
c.lineTo(x,u);
}
else
{
c.moveTo(0,x);
c.lineTo(u,x);
}
c.stroke();
c.closePath();
}
}