Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

插入排序

Sunny-117 opened this issue · comments

插入排序
Array.prototype.insertionSort = function () {
  for (let i = 1; i < this.length; i++) {
    const temp = this[i];
    let j = i;
    // 寻找插入的位置
    while(j > 0) {
      if (this[j - 1] > temp) {
        this[j] = this[j - 1];
      } else {
        break;
        // 此时j为应该插入的位置
      }
      j--;
    }
    this[j] = temp;
  }
}
// 时间复杂度O(n^2)
const arr = [2,5,4,8,2,1];
arr.insertionSort();