<script language="javascript">
<!--
var oMyArray = new Array( 13, 55, 37, 33, 45, 9, 60, 21, 10 );
document.write( "排序前: " + oMyArray );
for (i=0;i<oMyArray.length;i++)
{
for( index=0;index<oMyArray.length-i-1;index++)
{
if( oMyArray[index] <oMyArray[i] )
{
nTemp = oMyArray[index];
oMyArray[index] = oMyArray[i]
oMyArray[i] = nTemp;
}
}
}
document.write(" <br>排序后: " + oMyArray );
-->
</script>
<!--
var oMyArray = new Array( 13, 55, 37, 33, 45, 9, 60, 21, 10 );
document.write( "排序前: " + oMyArray );
for (i=0;i<oMyArray.length;i++)
{
for( index=0;index<oMyArray.length-i-1;index++)
{
if( oMyArray[index] <oMyArray[i] )
{
nTemp = oMyArray[index];
oMyArray[index] = oMyArray[i]
oMyArray[i] = nTemp;
}
}
}
document.write(" <br>排序后: " + oMyArray );
-->
</script>
{
document.write(i+" "+index+"<br>"); //加上这句,你自己看每次的索引
if( oMyArray[index] <oMyArray[i] )
<script>
var a = [13,55,37,33,45,9,60,21,10];
alert(a.sort(function(a,b){return a-b;}))
</script>改善冒泡
<script>
var a = [13,55,37,33,45,9,60,21,10];
alert(BubbleSort(a));
function BubbleSort(a)
{
var change=true;
for(var i=a.length-1;i>=0&&change;i--)
{
change = false;
for(var j=0;j<i;j++)
{
if(a[j+1]<a[j])
{
var t=a[j+1];
a[j+1]=a[j];
a[j] = t;
change = true;
}
}
}
return a;
}
</script>