📜  javascript 插入排序 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:13.456000             🧑  作者: Mango

代码示例1
const insertionSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i < arr.length; i++) {
    for (let j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
      [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
    }
  }
  return arr;
};

console.log(insertionSort([4, 9, 2, 1, 5]));