SOURCE

const data = [1, 3, 5, 7];

function halfSort(array, target) {
    let left = 0, right = array.length - 1;
    let index = array.length;
    while (left <= right) {
        let half = Math.floor((left + right) / 2);
        if (array[half] === target) {
            return half;
        } else if (array[half] < target) {
            index = half + 1;
            left = half + 1;
        } else {
            index = half;
            right = half - 1;
        }
    }
    return index;
}

console.log(halfSort(data, 9))
console 命令行工具 X clear

                    
>
console