23、插入排序

类似于抓牌

let ary = [23,31,4,7,3,9]

function insert(ary){
  let handle = [];
  handle.push(ary[0])
  for(let i = 1;i<ary.length;i++){
    for(let j = handle.length-1;j>=0;j--){
      if(ary[i]>handle[j]){
        handle.splice(j+1,0,ary[i]);            //把ary[i]放到handle[j]的后面;及放到handle[j+1]前面
        break;
      }
      if(j===0){
        handle.unshift(ary[i]);        //已经比较到最前面就插入数组首部
      }
    }
  }
  return handle;
}

results matching ""

    No results matching ""