function bubbleSort(arr){
for(let i = 1; i < arr.length; i++){
for(let j = 0; j <= i; j++){
if(arr[j] > arr[j+1]){
const temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return arr
}
arr2 = [1,2,3,4];
[arr2[0],arr2[1]] = [arr2[1],arr2[0]] //ES6解构赋值
console.log(arr2) // [2, 1, 3, 4]
function bubbleSort(arr){
for(let i = 1; i < arr.length; i++){
for(let j = 0; j <= i; j++){
if(arr[j] > arr[j+1]){
[arr[j],arr[j+1]] = [arr[j+1],arr[j]]
}
}
}
return arr
}
function bubbleSort2(arr) {
let i = arr.length - 1;
while (i > 0) {
let pos = 0;
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
pos = j;
[arr[j],arr[j+1]] = [arr[j+1],arr[j]]
}
}
i = pos;
}
return arr;
}
function bubbleSort3(arr) {
let start = 0;
let end = arr.length - 1;
while (start < end) {
for (let i = start; i < end; i++) {
if (arr[i] > arr[i + 1]) {
[arr[j],arr[j+1]] = [arr[j+1],arr[j]];
}
}
end -= 1;
for (let i = end; i > start; i--) {
if (arr[i - 1] > arr[i]) {
[arr[j],arr[j+1]] = [arr[j+1],arr[j]];
}
}
start += 1;
}
return arr;
}