function fn(arr) {
for (var i = arr.length; i >= 2; --i) {
for (var j = 0; j < i - 1; j++) {
if (arr[j + 1] < arr[j]) {
var temp;
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return arr
}
(2) 常规方法
function fn(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
var cur = arr[j];
arr[j] = arr[i];
arr[i] = cur;
}
}
}
return arr;
}
var array = [3, 2, 6, 2, 6, 1, 8];
console.log(fn(array)); //顺序
console.log(fn(array).reverse()); //倒序