Binary search principle is very simple, we have learned in high school is not much to say the following two-point code
in an already ordered array D, find a number with a size of num, and the length of the array is Len
1 intErfen (intNumintLen//in the already determined sequence.2 {3 intLeft,right,mid;//find a number that can be replaced or push back one4left=1;5right=Len;6Mid= (left+right)/2;7 while(left<=Right )8 {9 if(d[mid]<num)TenLeft=mid+1; One Else if(d[mid]>num) Aright=mid-1; - Else - returnmid; theMid= (left+right)/2; - } - returnLeft ; -}This is a function of their own writing, feeling that they do not write as much as the STL function library, the following is attached, STL functions in the binary lookup of various functions. The functions of binary lookup in STL are three Lower_bound, Upper_bound, Binary_search. These three functions are used in an ordered interval (which is, of course, the premise of using a binary search). iterators provide access to objects in a container, and define the scope of the objects in the container. An iterator is like a pointer. In fact, C + + pointers are also an iterator. However, iterators are not just pointers, so you can't think they must have address values. An array index, for example, can also be thought of as an iterator. The functions of binary lookup in STL are three Lower_bound, Upper_bound, Binary_search. These three functions are used in an ordered interval (which is, of course, the premise of using a binary search). where value is found, then Lower_bound returns an iterator that points to the first of the elements. Upper_bound returns an iterator that points to the next position of the last element in it (the explicit point is to return to the last position of value without destroying the order). If the value you are looking for does not exist, then both Lower_bound and Upper_bound return "where this element should appear when it is assumed to exist." To point out is that Lower_bound and Upper_bound in the source code only to change the order of the if-else sentence to determine the conditions, resulting in the final iterator position different effects. Binary_search attempts to find the element value in the sorted [First,last], returns True if it exists, and returns false if it does not exist. Returning a purely Boolean value may not satisfy the requirements, while Lower_bound, Upper_bound can provide additional information. In fact, the source shows that Binary_search is the use of Lower_bound to find out where the element should appear, and then compare the value of the location and value. There are two versions of this function, one is operator<, and the other is a comparison using the faux function comp. first on Lower_bound it can either return the first value that is larger than the lookup number, or return the position of the first number, with the code that returns the position appended to it .
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <iostream>5#include <algorithm>6#include <queue>7#include <vector>8#include <Set>9#include <stack>Ten#include <string> One#include <sstream> A#include <map> -#include <cctype> - using namespaceStd//binary Search, the efficiency is very high, the following is the STL binary lookup code the intMain () - { - inta[Ten]; - for(intI=0;i<Ten; i++) +a[i]=2*i; - intN; +Sort (a,a+Ten); A intLocation ; at while(SCANF ("%d",&N)) - { -Location=lower_bound (a,a+Ten, n)-A;//If the location is a pointer, the return is the value, if not the pointer, the return is the position -printf"%d\n", location); - } - return 0; in}
Fa
the case of the return value
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<Set>#include<stack>#include<string>#include<sstream>#include<map>#include<cctype>using namespacestd; intMain () {inta[Ten]; for(intI=0;i<Ten; i++) A[i]=2*i; intN; Sort (A,a+Ten); int*Location ; while(SCANF ("%d",&N)) { location=lower_bound (a,a+Ten, n);//The downside is that if the number you are looking for is too large, the larger number is returned.printf"%d\n",*Location ); } return 0;}The following is a Upper_bound application method (Upper_bound returns an iterator that points to the next position of the last element in it (the explicit point is to return to the last position of the value in the case that does not break the order)the rest are the same
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<Set>#include<stack>#include<string>#include<sstream>#include<map>#include<cctype>using namespacestd;intMain () {inta[Ten]; for(intI=0;i<Ten; i++) A[i]=5; intN; Sort (A,a+Ten); intLocation ; while(SCANF ("%d",&N)) { location=upper_bound (a,a+Ten, n)-A;//The downside is that if the number you are looking for is too large, the larger number is returned.printf"%d\n", location); } return 0;}
Two-part method