//基础版 time1: 0.0849609375 ms
function maopao(...arry) {
let arr = arry
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let num = arr[i]
arr[i] = arr[j]
arr[j] = num
}
}
}
return arr
}
const res= maopao(1,9,6,8,10,6,4)
console.log(res);
//优化版 time1: 0.005859375 ms
function maopao(...arry) {
let arr = arry
console.time('time1')
for (let i = 0; i < arr.length - 1; i++) {
let fring = true
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let num = arr[i]
arr[i] = arr[j]
arr[j] = num
fring = false
}
}
if (fring) {
break
}
}
console.timeEnd('time1')
return arr
}
const res= maopao(1,9,6,8,10,6,4)
console.log(res);
07-25
211
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-23
2492
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22
367
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-21
359
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)