二分法查找

function getIndex(arr,num){
        let start=0
        let end=arr.length-1
        while(start<=end){
            var mid=Math.floor((start+end)/2)
            if(num==arr[mid]){
                return mid
            }else if(num>arr[mid]){
                start=mid+1
            }else{
                end=mid-1
            }
        }
        return arr
    }
    var ary=[1,4,7,8,12,34,67,88,99,100]
    console.log(getIndex(ary,7))

你可能感兴趣的:(javascript)