javascript| algorithm
Before learning bubble algorithm, always do not know the relationship between N and N-1, and other variables, the reason is not to understand its true meaning, today wrote a bubble algorithm of the JS applet, and finally figured out.
var r1=new Array ();
r1[1]=35;
r1[2]=55;
r1[3]=65;
r1[4]=20;
r1[5]=30;
r1[6]=25;
r1[7]=0;
r1[8]=7;
r1[9]=5;
r1[10]=3;
var r2=new Array (35,55,65,20,30,25,0,7,5,3);
var r3=new Array (35,55,65,20,30,25,0,7,5,3);
function BubbleSort1 ()
{
var n=r1.length-1;
for (Var i=1;i<n;i++)
{
var Flag=false;
for (Var j=n-1;j>=i;j--)
{
var temp;
if (R1[j+1]<r1[j])
{
TEMP=R1[J+1];
R1[J+1]=R1[J];
R1[j]=temp;
}
Flag=true;
}
if (!flag) return;
}
}
function BubbleSort2 ()
{
var n=r2.length;
for (Var i=0;i<n-1;i++)
{
var Flag=false;
for (Var j=n-2;j>=i;j--)
{
var temp;
if (R2[j+1]<r2[j])
{
TEMP=R2[J+1];
R2[J+1]=R2[J];
R2[j]=temp;
}
Flag=true;
}
if (!flag) return;
}
}
function BubbleSort3 ()
{
var n=r3.length;
for (Var i=0;i<n-1;i++)
{
var Flag=false;
for (Var j=n-1;j>i;j--)
{
var temp;
if (R3[j]<r3[j-1])
{
TEMP=R3[J];
R3[J]=R3[J-1];
R3[j-1]=temp;
}
Flag=true;
}
if (!flag) return;
}
The HTML file code that calls them is as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 "/>
<title>BubbleSort</title>
<script language= "javascript" src= "Bubblesort.js" ></script>
<script language= "JavaScript" >
BubbleSort1 ();
BubbleSort2 ();
BubbleSort3 ();
for (Var z=1;z<11;z++)
{
document.write (R1[z]);
}
document.write ("<br/>");
for (Var k=0;k<10;k++)
{
document.write (R2[k]);
}
document.write ("<br/>");
for (Var k=0;k<10;k++)
{
document.write (R3[k]);
}
</script>
<body>
</body>