// 冒泡排序
function popSort(arr) {
if (arr.length < 2) return arr;
let n = arr.length;
while (n > 1) {
let exchanged = false;
for (let i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
exchange(arr, i, i + 1);
exchanged = true;
}
}
n--;
if (!exchanged) break;
}
return arr;
}
//插入排序
function insertSort(arr) {
if (arr.length < 2) return arr;
let n = 1;
while (n < arr.length) {
let val = arr[n];
let i = n-1;
for (; i>= 0; i--) {
let prev = arr[i];
if (val<prev) {
arr[i+1] = arr[i]
}else {
break;
}
}
arr[i+1] = val;
n++;
}
return arr;
}
//希尔排序
function shellSort(arr){
if (arr.length < 2)
几种常见排序算法的实现及运行时间对比
最新推荐文章于 2022-07-05 17:31:59 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)