向Array中添加选择排序

时间:2023-03-09 19:17:37
向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;
});

相关文章