【二分搜索】

    search(arr,target){
    //二分搜索:
    let count = 1;
    let start = 0;
    let end = arr.length - 1;
    while(start < = end){
      //取出中间值
      let middle = Math.floor((start + end)/2);
      let guess = arr[middle];
      //如果中间 == 目标值
      if(guess == target){
        return middle;//返回位置
      }
      if(guess>target){
        end = middle;
      }
      if(guess < target){
        start = middle+1
      }
      count++
    }
    return -1;
  }

你可能感兴趣的:(算法,数据结构,排序算法)