标签:io ar sp for java on log bs cti
冒泡算法:
1,
var array = [1,3,5,2,4,6,9,7,8];
var fun = function(arr){
var j = 0, tem, len = arr.length;
while(len > 0){
for(;j<len-1;j++){
if(arr[j] > arr[j + 1]){
tem = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tem;
}
}
len--;
}
return arr;
};
console.log(fun(array));
2,
function xx(array) {
for (var i = 0, len = array.length; i < len; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (array[j] > array[j + 1]) {
array[j] = [ array[j + 1], array[j + 1] = array[j]][0]; //没看明白同事写的
}
}
}
return array;
}
标签:io ar sp for java on log bs cti
原文地址:http://www.cnblogs.com/sunhw360/p/4155780.html