const arr1 = [0, 1, 6, 14, 10, 5, 7, 2];
const bubbleSort = (arr) => {
let max = arr[0];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
max = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = max;
}
}
}
return arr;
};
// 从小到大排序
const result = bubbleSort(arr1)
console.log(11, result);
js实现冒泡排序
最新推荐文章于 2020-12-03 13:50:54 发布