//冒泡排序
let arr = [2, 4, 1, 6, 3]
function bubbled(arr) {
for (let i = 0; i < arr.length - 1; i++) {
//【!!注意】这里不是j=i,因为回回都必须重头遍历,才能不漏一个嘛~
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
return arr
}
console.log(bubbled(arr)); //[1,2,3,4,6]
03-07
8765
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-10
542
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-10
279
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-10
271
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)