22、冒泡排序

数组当前项跟后一项进行对比,如果当前项比后一项大那么两项交换位置

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

function bubble(ary){
  for(let i = 0;i<ary.length-1;i++){    //控制比较轮数
    for(let j = 0;j<ary.length-1-i;j++){        //每一轮比较的次数
      ary[j]>ary[j+1]?[ary[j],ary[j+1]]=[ary[j+1],ary[j]]:null
    }
  }
  return ary
}

results matching ""

    No results matching ""