js 插入排序 时间复杂度 n^2

// 插入排序  时间复杂度  n^2
var arr = [1, 4, 5, 6, 8, 9, 4, 3, 3, 4, 56, 7, 8, 56, 4, 3, 2, 3, 5, 456]

for (let i = 2, len = arr.length; i < len; i++) {
    let k = arr[i]
    let index = i
    for (let j = i - 1; j > 0; j--) {
        if(k < arr[j]) {
            let t = arr[index]
            arr[index] = arr[j]
            arr[j] = t
            index = j
        }
    }

}

console.log(arr)

你可能感兴趣的:(算法)