SOURCE

// 二分查找,必须是已排序好的列表
let array = [ 12, 33, 34, 48, 57, 63, 66, 71, 80, 89 ];
function search(arr, item){
    let low = 0, high = arr.length - 1;
    while(low <= high){  // 注意必须包含=,否则会非法数据
        // 确定查找范围
        let mid = Math.round((low + high)/2);
        let guess = arr[mid];
        if(guess == item){
            return mid;
        }
        if(guess < item){
            low = mid + 1;
        }else{
            high = mid - 1;
        }
    }
    return '非法数据'
}

console.log(search(array, 48));
console 命令行工具 X clear

                    
>
console