Binary search in C++ STL

Keywords: less

Binary search in C++ STL


Binary search is suitable for sequences with certain sequence

  1. Head file:
  #include<algorithm>
  1. usage method:
    a. Function template: binary search (arr [] + size, indx)
    b. Parameter Description:
    arr []: array first address
    size: number of array elements
    indx: value to find
    c: Function function: search by dichotomy in array. If indx element is found in array (array element is required to be non decreasing), it is true. If it is not found, the return value is false.
    Find success, return 1; usage example:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100]= {4,10,11,30,69,70,96,100};
    int b=binary_search(a,a+9,11);//Find successful, return 1
    cout<<b<<endl;
    int c=binary_search(a,a+9,40);//Failed to find, return 0
    cout<<c<<endl;
    int d=lower_bound(a,a+9,10)-a;
    cout<<d<<endl;
    int e=lower_bound(a,a+9,101)-a;
    cout<<e<<endl;
    int f=upper_bound(a,a+9,10)-a;
    cout<<f<<endl;
    int g=upper_bound(a,a+9,101)-a;
    cout<<g<<endl;
}

Output results:

1
0
1
9
2
9

Process returned 0 (0x0)   execution time : 0.097 s
Press any key to continue.

2.lower_bound: find the location of the first element greater than or equal to.
a. Function template: lower Gu bound (arr [] + size, indx):
b. Parameter Description:
arr []: array first address
size: number of array elements
indx: value to find
c. Function function: the function lower_bound() performs binary search in the front closed and back open intervals of the first and last, and returns the first element position (note the address) greater than or equal to val. If all elements are less than Val, return the location of last
d. For example:
The number sequence of an array is: 4,10,11,30,69,70,96100. If the number to be inserted 3,9111.pos is the subscript of the position to be inserted, then
/ note that since the return value is a pointer, the pointer minus the array is an int variable/
POS = lower_bound (number, number + 8, 3) - number, pos = 0.
POS = lower_bound (number, number + 8, 9) - number, pos = 1, that is, the position where the subscript of the number array is 1 (that is, the position where 10 is located).
POS = lower_bound (number, number + 8, 111) - number, pos = 8, that is, the position where the subscript of the number array is 8 (but the subscript upper limit is 7, so the next element of the last element is returned).
e. Note: the function lower_bound() performs a binary search in the first and last pre closed and post open intervals to return the first element position greater than or equal to val. If all elements are less than Val, the last position is returned, and the last position is out of bounds!

Returns the first pluggable location of the lookup element, that is, the location of the first element of "element value > = lookup value"

3. Upper "bound: find the first location larger than an element.
a. Function template: upper_bound (arr [] + size, indx):
b. Parameter Description:
arr []: array first address
size: number of array elements
indx: value to find
c. Function function: function upper bound() returns the upper bound of the keyword searched in the open interval before closing and after closing, and returns the first element position greater than val
For example: after an array number sequence 1,2,2,4.upper'bound (2), the returned position is 3 (subscript), that is, 4. Similarly, if the inserted element is larger than all the elements in the array, last is returned. (Note: array subscript out of bounds)
Returns the last pluggable location of the lookup element, that is, the location of the first element of element value > find value.

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100]= {4,10,11,30,69,70,96,100};
    int b=binary_search(a,a+9,11);//Find successful, return 1
    cout<<b<<endl;
    int c=binary_search(a,a+9,40);//Failed to find, return 0
    cout<<c<<endl;
    int d=lower_bound(a,a+9,10)-a;
    cout<<d<<endl;
    int e=lower_bound(a,a+9,69)-a;
    cout<<e<<endl;
    int f=upper_bound(a,a+9,10)-a;
    cout<<f<<endl;
    int g=upper_bound(a,a+9,69)-a;
    cout<<g<<endl;
}

Output results:

1
0
1
4
2
5

Process returned 0 (0x0)   execution time : 0.071 s
Press any key to continue.


Binary search code:

#include <cstdio>
#include <algorithm>
#include<iostream>
using namespace std;
int main()
{
    int a[10]= {4,10,11,30,69,70,96,100};
    int left=0,mid,right=10-1,i,key=30;  //Find 30;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(a[mid]==key)
        {
            cout<<mid<<endl;
            break;
        }
        else if(a[mid]>key)
             {
                 right=mid-1;
             }
             else
             {
                 left=mid+1;
             }
    }
    return 0;
}

Operation result:

3

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.


Published 3 original articles, won praise 5, visited 42
Private letter follow

Posted by jlhaslip on Wed, 05 Feb 2020 01:51:46 -0800