思路
Array.prototype.insertSort = function () { for (let i = 1; i < this.length; i++) { const temp = this[i]; let j = i; while (j > 0) { if (this[j] < this[j - 1]) { this[j] = this[j - 1]; j--; } else { break } } this[j] = temp; } } const arr1 = [6, 8, 5, 9, 3, 2, 1] arr1.selectSort() console.log(arr1)
时间复杂度:O(n^2)
空间复杂度:O(1)