您的位置:首页 > 其它

向Array中添加冒泡排序

2013-10-09 13:10 162 查看

冒泡排序思想

通过在无序区的相邻元素的比较和替换,使较小的元素浮到最上面。

冒泡排序实现

Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
Array.method('bubbleSort', function(){
var len = this.lenght,
i, j, tmp;
for(i=0; i<len; i++){
for(j=len-1; j>i; j--){
if(this[j] > this[j-1]){
tmp = this[j-1];
this[j-1] = this[j];
this[j] = tmp;
}
}
}
return this;
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: