向Array中添加选择排序

选择排序思路

在无序区中选出最小的元素,然后将它和有序区的第一个元素交换位置。

选择排序实现

Function.prototype.method = function(name, func){
    this.prototype[name] = func;
    return this;
};

Array.method('selectSort', function(){
    var len = this.length,
        i, j, k, tmp;
    for(i=0; i<len; i++){
        k = i;
        for(j=i+1; j<len; j++){
            if(this[j] < this[k]) k = j;
        }
        iff(k!=i){
            tmp = this[k];
            this[k] = this[i];
            this[i] = tmp;
        }
    }
    return this;
});

 

posted @ 2013-10-09 13:43  JChen___  阅读(133)  评论(0编辑  收藏  举报