Binary search extremely variants

The core idea of ​​the traditional binary search is to keep shrinking on the scale of 1/2 under the condition that the target value is in the interval! ! ! !

Note: The proof of thinking is the same, whether all left or right will cross a certain value (usually the target value)

1. Basic binary search

       int left=0,right=A.length-1,mid=0;
       while(left<=right){
            mid=(left+right)/2;
            if(A[mid]==target){
                break;
            }else if(A[mid]>target){
                right=mid-1;
            }else if((A[mid]<target){
                left=mid+1;
            }
       }

Proof: If there is a target value, the subscript of the target value will be returned

考虑序列:....7 8 9......查找8

只需要证明在找到8之前left,right永远不会越过8这个数,可用反证法证明。如果left由小于8的数指向了8之后的数,
那么left一定是执行了left=mid+1,那么A[mid]一定是小于8的,也就是说A[mid]最大只能是7,那么新的A[left]最多只能是
8,不可能超过8.right同理。

一句话总结就是[left,right] 区间里永远都包含8这个数,区间不断以1/2的规模收缩

Proof: If there is no target value, it will return the index of the element that is less than the target value that is closest to the target value, or the index of the element that is greater than the target value that is closest to the target value.

考虑序列: .....7 9.......查找8
只需要在循环时证明在[left,right]这个区间只要是一个正常的区间即left<right,left最多指向9,而left一旦指向了9,right就会不断往左边收缩,最后返回返回mid=9。同理 right最多指向7,如果right先指向了7,最终返回mid=7.

Second, the binary search of the left boundary

        int left=0,right=A.length-1,mid=0;
        while(left<=right){
            mid=(left+right)/2;
            if(A[mid]<target){
                left=mid+1;
            }else{
                right=mid-1;
            }
        }

Proof: If there is a target value, it will return the subscript of the leftmost target value or the subscript of its predecessor

考虑序列 .....7 8 8 8 9......

我们只需要证明,[left,right] 区间里永远都包含第一个8或者7这个数,而且区间不断以1/2的规模收缩

证明思路是left永远不会越过第一个8,right永远不会越过7,可用反证法证明,类比上面的证明

Third, the binary search of the right boundary

        int left=0,right=A.length-1,mid=0;
        while(left<=right){
            mid=(left+right)/2;
            if(A[mid]<=target){
                left=mid+1;
            }else{
                right=mid-1;
            }
        }

Guess you like

Origin blog.csdn.net/qq_41634872/article/details/110227317