const bubbleSort = arr => {
const list = arr.slice();
const len = list.length;
for (let i = 0; i < len; i++) {
for (let j = len - 1; j > i; j--) {
if (list[j] < list[j - 1]) {
const tmp = list[j - 1];
list[j - 1] = list[j];
list[j] = tmp;
}
}
}
return list;
}
const bubbleSort2 = arr => {
const list = arr.slice(); //保证函数为纯函数
const len = list.length;
for (let i = 0; i < len; i++) {
let exchange = false;
for (let j = len - 1; j > i; j--) {
if (list[j] < list[j - 1]) {
const tmp = list[j - 1];
list[j - 1] = list[j];
list[j] = tmp;
exchange = true;
}
}
if (!exchange) return list
}
return list;
}