C++ Algorithm - Binary Search

Introduction

English name: Binary Search
is also called binary search, which is a more efficient search method. However, the binary search requires that the linear table must adopt a sequential storage structure, and the elements in the table are arranged in order according to the key

step

The following uses the array 1,2,3,4,5,6,7,8,9 as an example.
At this point we search for 3

1. First of all, our search range is all, and the average value of the subscripts at both ends is taken
  • First, the subscripts at both ends are 0 and n-1
    insert image description here
  • At this time, the intermediate value 5 is obtained
2. Compare the two numbers and narrow down the search range
  • After comparison, the obtained middle value is greater than the number to be found, so the number we are looking for should be on the left side of the middle value, so the range is changed from the original left side to the number before the middle value
    insert image description here
3. Repeat steps 1 and 2 until the number is found or cannot be found
  • We continue with the steps above
    insert image description here- this time the lookup number is larger, so this time we zoom out to the right
    insert image description hereinsert image description here

  • At this point we have found it, and we can return to the subscript

  • Next, we can't find it, we will remove 3 from the array, and then search again

  • insert image description hereinsert image description here
    insert image description here

  • At this time, the two sides have merged into one number, but it is still not, so it can be judged that there is no such number. Pay attention to the fact that the subscripts on both sides will still change, and then return -1
    insert image description here

the code

  • From the above process we can see that we need two variables to mark the left and right sides, so we create low and high

insert image description here

  • Then the intermediate value of each calculation, we use mid to represent
    insert image description here
  • Then we change the values ​​of low and high according to the comparison result (one at a time)
    insert image description hereinsert image description here
  • As above, finally low and high will be merged (note that they should also be compared when merging). At this time, we find that it is not the same after comparison. According to the law, at this time, high will be in front of low. Only then can it be determined whether there is no, so the condition of this cycle is low<=high
#include<bits/stdc++.h>

using namespace std;

int BinarySearch(int a[],int low,int high,int target)
{
    
    
    while(low<=high)
        {
    
    
            int mid=low+(high-low)/2;//溢出问题
            cout<<"low:"<<low<<" high:"<<high<<" mid:"<<mid<<endl;
            if(a[mid]>target)
                high=mid-1;
            else if(a[mid]<target)
                low=mid+1;
            else
                return mid;

        }
    return -1;
}


int main()
{
    
    
    int a[10]={
    
    1,2,3,4,5,6,7,8,9};
    int len=9;
    cout<<BinarySearch(a,0,len-1,3)<<endl;
    return 0;
}










characteristic

time complexity

Time complexity can be expressed as O ( log 2 n ) O(log2n)O ( log 2 n ) _ _

Quiz

No need to change the code this time, just come to a question

All the numbers in an increasing array of length n-1 are unique, and each number is in the range of 0~n-1. Among the n numbers in the range, there is only one number that is not in the array. Please find this number

The input data is n and the array

Input: 9 0 1 2 4 5 6 7 8
Output: 3

Input: 6 0 1 2 3 4
Output: 5

Input: 4 1 2 3
Output: 0

There are many ways to solve this question, but here the test uses dichotomous points to solve the problem

New tricks are coming (Θ▽Θ)
By the way, if you are interested, you can learn the rule of thirds

Guess you like

Origin blog.csdn.net/qq_44616044/article/details/115731497