function bubbleSort(arr) {
let n = arr.length;
for (let i = 0; i < n-1; i++) {
for (let j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// 交换 arr[j+1]和 arr[j]
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
// 测试
let arr = [64, 34, 25, 12, 22, 11, 90];
console.log("排序前的数组为:");
console.log(arr.join(" "));
arr = bubbleSort(arr);
console.log("排序后的数组为:");
console.log(arr.join(" "));