function bubbleSort(target) {
let temp = null
for(let i = 0; i < target.length - 1; i++) {
for(let j = target.length - 1; j > i; j--) {
if (target[j] < target[j - 1]) {
temp = target[j]
target[j] = target[j - 1]
target[j - 1] = temp
}
}
console.log(`第 ${i + 1} 次排序`)
}
return target
}
console.log(bubbleSort([23, 32, 5, 72, 12, 1]))
console.log(bubbleSort([1, 2, 3, 4, 5, 6]))
function bubbleSort(target) {
let temp = null
let flag = false
for(let i = 0; i < target.length - 1; i++) {
for(let j = target.length - 1; j > i; j--) {
if (target[j] < target[j - 1]) {
temp = target[j]
target[j] = target[j - 1]
target[j - 1] = temp
flag = true
}
}
console.log(`第 ${i + 1} 次排序`)
if (!flag) break
}
return target
}
console