【Binary Search Algorithm】|Binary search, there is such a fast query method?|Two ways of binary search

1. The way of searching and querying

When we want to query a certain fixed value, the first thing we think of is a basic simple traversal idea, which is to traverse all the content for a whole time. This method can undoubtedly complete this traversal , but the time complexity of this method is as high as O(n) . This time complexity is quite unfriendly to multiple search operations. As we all know, O(n 2 ) will always be used when the data range is relatively large. Explosive time, so we have to think of a more convenient way to simplify this operation.

When the data has a certain sequence relationship, we can use this so-called relationship to compare the size and delineate its existing interval, so that we can traverse half of the data each time, so we call this search method binary search . The essential meaning is to gradually delineate the range for this value, so as to determine the solution of this value.
insert image description here

Second, the implementation process of the binary algorithm

1. Dichotomous thinking

It is very obvious that the binary search is shortening the interval to this target value, but how can this target be an interval with several identical values? There will be a difference between the two writing methods of our binary algorithm. Here I like to use the first two points Search and post-binary search to call them, they will return different values ​​only when there are multiple target values, and return is the same operation when there is no target value or one target value

An example where the target value is not 1
bold style
Of course, there is another important point in the binary search: there must be an order relationship between the values ​​​​of the binary search operation, not just limited to the size relationship .

2, the code implementation process

First, let's take a look at the data that our binary algorithm needs to import.

int bsearch_1(int l ,int r, int x)
//l:最开始的下标  ,r:是最结尾的下标  x :是查找的数值

When this step is completed, we need to perform a binary search operation

1. Top binary search
while (i < j)
{
    
    
	int mid = (l + r) >> 1 ;
	//中间值mid 前搜就是最简单的 l + r
	if(q[mid] >= x)	r = mid ;
	else l = mid + 1 ;
	//前搜要控制后面的值为 = 就是 r = mid 
}
2. Post binary search
while(i < j)
{
    
    
	int mid = l + r + 1 >> 1;
	if(q[mid] <= x )	l = mid ;
	else r = mid - 1 ;
}

After completing the search operation, we need to check whether the value is the value we requested

if(q[r] != x)
	return - 1;
//当搜索不到的时候返回 - 1 ;
else 
	return r; //搜索的最后 r == l

3 , total code

#include <iostream>

const int N =100010;

int n , m ,q[N] ;

int  bsearch_1(int l ,int r ,int x)
{
    
    
	while (l < r)
	{
    
    
		int mid = r + l >> 1;
		if(q[mid] >= x )	r = mid ;
		else l = mid + 1 ;
	}
	if(q[l] != x )
		return - 1 ;
	else return l;
}

int bsearch_2 (int l ,int r ,int x )
{
    
    
	while (l < r)
	{
    
    
		int mid  = (l + r + 1 ) >> 1 ;
		if(q[mid] <= x)  l = mid ;
		else r = mid - 1; 
	}
	if(q[l] != x )
		return - 1 ;
	else return l ;
}
//两搜索的差别就是一个前查找,另一个为后查找
using namespace std;
int main ()
{
    
    
    cin >> n >> m;
    for(int i = 0 ; i < n ; i ++ )
        cin >> q[i];
    while ( m -- )
    {
    
    
        int n1;
        cin >> n1 ;
        cout << bsearch_1( 0 , n - 1 , n1 )<<" "<<bsearch_2( 0 , n - 1 , n1 )<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/wen030803/article/details/131679132
Recommended