大的下沉,轻的冒泡
const arr = [11, 3, 6, 2, 9];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
const beforeSort = JSON.stringify(arr);
const temp = arr[j + 1];
if (arr[j] > arr[j + 1]) {
arr[j + 1] = arr[j];
arr[j] = temp;
}
console.log(`<---第${j + 1}次: 比较前${beforeSort} 比较后得出${JSON.stringify(arr)} 本次对比${arr[j]}、${arr[j + 1]} `);
}
console.warn(`第${i + 1}趟:比较后得出${JSON.stringify(arr)}`);
}
console.error(`最终得出${JSON.stringify(arr)}`);
原文:https://www.cnblogs.com/dshvv/p/15233139.html