//冒泡排序
function bubbleSort(arr) {
let len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { //相邻元素两两对比
let temp = arr[j + 1] //元素交换
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
let arr = [5, 2, 1, 3, 6, 10, 8, 4, 7]
console.log(bubbleSort(arr)) // [ 1, 2, 3, 4, 5, 6, 7, 8, 10 ]
请开始你的表演...