JS 冒泡算法

<script type="text/javascript">
Array.prototype.swap
=function(i,j)
{
 
var temp=this[i];
 
this[i]=this[j];
 
this[j]=temp;
}
Array.prototype.bubbleSort
=function(comp)
{
 
var l=this.length;
 
for(var j=l-1;j>0;j--)
 {
  
for(var i=0;i<j;i++)
  {
   
if(comp(this[i],this[i+1]))
   {
    
this.swap(i,i+1);
   }
  }
 }
}
var a=[4,2,6,8,7,11,34,13,9];
a.bubbleSort(
function(x,y){return x>y;});
alert(a);
</script>
posted @ 2009-01-06 09:21  M'  阅读(595)  评论(0编辑  收藏  举报