# day24-使用js实现冒泡排序

使用JavaScript实现冒泡排序

// 测试数据

let arr = [5, 3, 8, 4, 6];

console.log(bubbleSort(arr)); // 输出:[3, 4, 5, 6, 8]

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]) {
                [arr[j],arr[j+1]] = [arr[j+1],arr[j]]
            }
        }
    }
    return arr
}
const arr = [5, 3, 8, 4, 6]
const res = bubbleSort(arr)
console.log(res,'res')
1
2
3
4
5
6
7
8
9
10
11
12
13
14