二分查找JS实现

function binary_search(array, key){
 let low = 0
 let high = array.length - 1
 while(low<=high){
  let mid = parseInt((high+low)/2)
  if(array[mid]===key){
   return mid 
  } else if(array[mid]<key) {
    low = mid + 1
  } else if(array[mid]>key) {
    high = mid - 1
  }
 }    
  return -1
}

 

posted @ 2020-10-09 20:42  想学JS的前端  阅读(113)  评论(0编辑  收藏  举报