1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-09 05:27:52 +08:00
Files
408CSFamily/code/ds/BubbleSort.js
最近在学桌球 6a1567cd2e feat: 更新文档
2023-08-30 17:04:20 +08:00

40 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 冒泡排序【JavaScript版本】
*/
function BubbleSort(arr, len) {
// 校正数组的长度
len = arr.length === len ? len : arr.length
// 冒泡排序让数组arr有序
for (let i = 0; i < len - 1; i++) {
let isSorted = false
// len个数组进行len-1趟一趟冒泡
for (let j = len - 1; j > i; j--) {
// 注意这里的for循环倒序是有讲究的想象一下泡泡不都是网上升的么....
if (arr[j - 1] > arr[j]) {
// 交换元素,始终让最小的元素往上走(冒泡)
const temp = arr[j - 1]
arr[j - 1] = arr[j]
arr[j] = temp
// 需要冒泡
isSorted = true
}
}
// 第一趟比较后,如果本身序列是有序的,就直接跳出循环
if (isSorted === false) {
break
}
}
return arr
}
const initArr = [1, 5, 8, 3, 2, 9, 16]
console.log(`冒泡排序前:${initArr}`)
const sortedArr = BubbleSort(initArr, 7)
console.log(`冒泡排序后:${sortedArr}`)