Three JavaScript representations of the bubble Algorithm

Source: Internet
Author: User

Previous learning bubble Algorithm The relationship between N, n-1, and other variables is always unclear, because it does not understand the true meaning of it. Today I wrote a JS small bubble Algorithm Program And finally figured it out. CopyCode The Code is as follows: 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 );
VaR R3 = new array );

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 ()
{< br> var n = r2.length;
for (VAR I = 0; I {< br> var flag = false;
for (var j = n-2; j> = I; j --)
{< br> var temp;
If (R2 [J + 1] {
temp = R2 [J + 1];
R2 [J + 1] = R2 [J];
R2 [J] = temp;
}< br> flag = true;
}< br> If (! Flag) return;
}< BR >}< br> function bubblesort3 ()
{< br> var n = r3.length;
for (VAR I = 0; I {< br> var flag = false;
for (VAR J = n-1; j> I; j --)
{< br> var temp;
If (R3 [J] {
temp = R3 [J];
R3 [J] = R3 [J-1];
R3 [J-1] = temp;
}< br> flag = true;
}< br> If (! Flag) return;
}< BR >}

Code for calling their HTML files is as follows:Copy codeThe Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.01 // en" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<Head>
<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>
</Head>
<Body>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.