Data Structure - Binary Search Algorithm

1. Algorithm description

Binary search algorithm: Requirement: In the ordered array A, search for the value target, if found, return the index number of the target value, otherwise return -1

Algorithm Description
premise Given an ordered array A containing n elements, satisfying A0<=A1<=A2<=···<=An-1, a value target to be searched
1 Set i=0, j=n-1
2 If i>j, end the search, not found
3 Set m=floor((i+j)/2), m is the middle index, and floor is rounded down (that is, the largest integer less than or equal to (i+j)/2)
4 If target<Am, set j=m-1, jump to step 2
5 If target>Am, set i=m+1, jump to step 2
6 r If Am=target, end the search and find the target value

2. Algorithm implementation

//Java写一个方法,传入一个数组和要查找的目标值
public static int erfenSearch(int[] a,int target){
   
       int i=0,j=a.length-1;      //设置两个指针的初值
       while(i<=j){               //在i到j范围内查找
             int m=(i+j)>>>1;
             if(target<a[m]){     //目标在左边
                j=m-1;            

            }else if(a[m]<target){ //目标在右边
                   i=m+1;
            }else{                  //找到了
                   return m;
            }
             
             reutrn -1;

       }

}

3. Existing problems

1. Why write i<=j instead of i<j in the loop?

Answer: If you just write i<j, then only the elements whose index is m will participate in the comparison, and the elements indexed by i and j will not participate in the comparison. For example, when i=j is found when the last element is found, it will not be entered at this time Loop to find m, so there is no way to compare whether the element when i==j is the target value, and i==j means that the element pointed to by i and j will also participate in the comparison

2. Why is (i+j)/2 written as >>>1?

Answer: If i and j become the addition of two large positive integers, the result may be negative. In binary, if the highest bit of the same binary number is regarded as the sign bit, then the obtained The result is negative. If the sign bit is not considered, then the result is normal. Change: use the unsigned right shift operator instead of dividing by 2.

Right shift: The 8-bit representation of the binary number of 7 is 0000 0111, and after the right shift, it becomes 0000 0011, that is, it becomes 3

The binary value of 16 is 0001 0000, after shifting right, it becomes 0000 1000, that is, it becomes 8

And this can be regarded as dividing by 2 and rounding, (7 divided by 2 and rounding is 3), the same binary may show positive integers, but also negative numbers, but by shifting it to the right, the final results are all is correct, and additionally, other languages ​​can be accommodated by this right-shifting

=(mi+j)>>>1;

3. Why is the less than sign written in the code? What are the benefits?

Answer: It is related to the sorting of the array. Whether the array is in ascending order or sorting, if it is in ascending order, write the less than sign, which can give people a more intuitive feeling. The written code is more consistent with our thinking, but of course you don’t want to write it like this. It doesn't matter, as long as you can understand it.

4. The revised binary search algorithm

//改写后的二分查找算法
public static int erfenSearch(int[] a,int target){
   
       int i=0,j=a.length;         //不进行减一
       while(i<j){                 //没有等于
             int m=(i+j)>>>1;
             if(target<a[m]){     
                j=m;               //直接等于m

            }else if(a[m]<target){ 
                   i=m+1;
            }else{              
                   return m;
            }
             
             reutrn -1;
       }
}

At this time, the position pointed to by j will not participate in the calculation. You can take an increasing array for demonstration. Both versions of the dichotomy need to be mastered

Note: After the revision, you cannot add an equal sign to the loop condition, otherwise it will fall into an infinite loop when looking for a target value that does not exist

Guess you like

Origin blog.csdn.net/MayMarch/article/details/130021648