欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js实现二分查找算法

程序员文章站 2024-03-20 09:58:46
...
  binarySearchIndex(array, target) {
    let high = array.length - 1
    let low = 0
    while (low <= high) {
      let mid = (high + low) >>> 1
      let midVal = array[mid]
      console.log('当前mid', mid)
      if (midVal == target) {
        return mid
      } else if (midVal > target) {
        high = mid -1
      } else {
        low = mid + 1
      }
    }
    return -1
  }
相关标签: 算法 js