Find the problem-solving report in half

Find the problem-solving report in half


The binary search is one of the search methods, and the commonly used search method is traversal search.
Binary search uses the idea of ​​dichotomy, which can also be called binary search. The idea is in the ordered array a( Must be in order, from small to large or from large to small. ) To find the specified element k, compare the middle element a[mid] of the array with k. If a[mid] is equal to k, then it has been found; if a[mid] is not equal to k, then a[mid] and k must be found according to a[ mid] and the size of k, search in the first half or the second half of the corresponding array, and keep narrowing the search range (the search range of the i-th time is half of the i-1th time), and you need to Recursive call Binary search function.
The binary search function can be expressed as:

int binaryfind(int a[],int k,int low,int high);

Among them, a[] represents the array to be searched, k represents the search element, low and high limit the search range in the array a, starting from the low-1 position to the high-1 position (because the array subscript starts from zero) , And mid=(low+high).
Note that
since there may be equal elements in array a, if it is required to output the position of the first occurrence of k in array a, you can still call the binary search function after finding the position of the element equal to element k for the first time until The search range is 1, then the position where the element k appears for the first time in the array a is found at this time.
example


There are n numbers in the title description (n<=1000000), and these n numbers have been stored in an array in ascending order, and then there are T queries, each time a number is entered, and the binary search method is required to find the number The position of the first occurrence of the number in the array. If it is not in the array, output 0.

Input
the number n of the first row of the array element
value of n array elements in the second row
the third row number of an input query T (T <= 100000)
down T-lines, a digital input line need to query each of the
output
values of the lookup Position in the array

Sample input
10
10 9 8 7 6 5 4 3 2 1
2
9
5
Sample output
2
6

This question is a typical binary search question, output the position of the search element in the array for the first time, write the binary search function, and then make a recursive call.
Attention to details
1. Due to the requirement that n<1000000, directly defining the array cannot apply for such a large array space, so the malloc function is used. (When the array space required in the question is relatively large, you must think of applying for space
as soon as possible ) 2. In the binary search function, how do you highlight that the search range has become 1? In fact, when the search range is 1, the satisfied condition is low=high. At this time, if a[mid]==k, it means that the first position of k in the array is found, otherwise k does not exist in the array. (Be sure to pay attention to this condition. If the condition is written incorrectly, some numbers will not be found)
Code implementation (may be flawed, redundant, and passed the OJ test)

#include<stdio.h>
#include<stdlib.h>
int binaryfind(int a[],int num,int low,int high) //二分查找函数的定义
{
    
    
	int mid;
	mid=(low+high)/2;
	while(low<high)
	{
    
    
		if(a[mid]==num) //即便查找到但不一定是第一次出现的位置,继续调用二分查找函数
 		{
    
    
			return binaryfind(a,num,low,mid);
		}
		else if(a[mid]>num)
		{
    
    
			return binaryfind(a,num,mid+1,high);
		}
		else if(a[mid]<num)
		{
    
    
			return binaryfind(a,num,low,mid-1);
		}
	}
	if(low==high && a[mid]==num)   //这个条件是重点,表明查找范围是1,并且a[mid]==num
	{
    
    
		return low+1;
	}
	return 0;   //查找不到返回0
}
int main()
{
    
    
	int i,m,n;
	int *a=(int *)malloc(1000001*sizeof(int));
	scanf("%d",&m);
	for(i=0;i<m;i++)
	{
    
    
		scanf("%d",&a[i]);
	}
	scanf("%d",&n);
	int num;
	for(i=0;i<n;i++)
	{
    
    
		scanf("%d",&num);
		printf("%d\n",binaryfind(a,num,0,m-1));
	}
	return 0;
}

(You must be proficient in understanding the principle of the algorithm, be able to write the code proficiently, and pay attention to the details, not to pursue the correct submission, but to think and get something)

Guess you like

Origin blog.csdn.net/m0_46772594/article/details/105696187