# 冒泡演算法的三種javascript表示

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;
}
}    調用它們的HTML檔案代碼如下：
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<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>
</html>
http://www.cnblogs.com/tyrael007/archive/2006/11/20/566375.html

## Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD