向Array中添加冒泡排序

冒泡排序思想

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

冒泡排序实现

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;
});
原文地址:https://www.cnblogs.com/JChen666/p/3359024.html