1. const array = [0, 3, 6, 17, 35, 100, 98, 66, 88, 58, 23, 45, 34, 11];
    2. //1.把数组分为[已排序]和[未排序]两部分,第一个数为[已排序],其余为[未排序]
    3. //2.从[未排序]抽出第一个数,和[已排序]部分比较,插入到合适的位置
    4. function insertSort(array) {
    5. const len = array.length;
    6. for (let i = 1; i < len; i++) {
    7. const curValue = array[i];
    8. let j;
    9. for (j = i - 1; j > -1 && array[j] > curValue; j--) {
    10. array[j + 1] = array[j];
    11. }
    12. //说明说明循环到array[j]位置且array[j]<curValue
    13. //已经找到位置了,那么将array[j+1]复制为curValue
    14. array[j + 1] = curValue;
    15. }
    16. return array;
    17. }
    18. console.log(insertSort(array));