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
}