该题js解法:
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function bubbleSort(arr) {
const 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]) {
// 交换两个元素
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
let n = 0;
let arr = [];
rl.on(‘line’, (input) => {
if (n === 0) {
n = parseInt(input);
} else {
arr = input.split(” “).map(Number);
bubbleSort(arr);
console.log(arr.join(” “));
rl.close();
}
});