插入排序

138 阅读1分钟
概念

插入排序是一种最简单直观的排序算法,它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

思路

1、将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。

2、从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。

代码
const arr = ['3','2','1','9','6']
console.log(insertionSort(arr))
function insertionSort(arr){
    const len = arr.length
    let preIndex,current
    
    for(let i = 1;i < len;i ++){
        preIndex = i - 1
        current = arr[i]
        
        while(preIndex >= 0 && arr[preIndex] > current){
            //交换
            arr[preIndex + 1] = arr[preIndex]
            preIndex --
        }
        
        arr[preIndex + 1] = current   
    }
    
    return arr
}

image.png