2020-12-25

Binary search

1. Basic concepts of binary search:

Binary search is also known as binary search. The binary search is to reduce the sequence of ordinal numbers to half of the original number until the first element of the element or the half area is higher than the tail element.
Enter 10 ordered numbers and the desired search Keyword, and then use the binary search to find the position of the keyword in the array

2. The general steps of binary search:

Binary search is an efficient search method, which can significantly reduce the number of comparisons and improve search efficiency. However, the prerequisite for binary search is that the elements in the lookup table must be in order

First of all, you must set 3 variables low, mid, high, respectively save the beginning, middle and end number of the array elements, assuming there are 10 elements, let low=0, high=9, mid=(low+high) /2=4
and then make the following judgments:
(1) If the value of the serial number mid is equal to the element x to be searched, it means that the data is found, and the corresponding serial number mid is returned
(2) If x<a[mid], it means to find The data of is located between low and mid-1 serial number, there is no need to find the element between mid and high. Therefore, the value of variable high is set to mid-1, and the data between low and mid-1
( 3) If a[mid]>x, it means that the element to be searched is between mid+1 and high, low=mid+1, re-find the data between mid+1 and high
(4) Step by step if it reaches low> When it is high, the target has not been found, it means that there is no such data in the array.
The basic idea of ​​the binary search is to arrange the sequence in an orderly manner. During the search process, the search is performed by jumping, that is, the middle position of the ordered array is the comparison object. If the element to be found is smaller than the element, the sequence to be checked is reduced to the left half, otherwise it is the right half, and the interval is reduced by half through a comparison

The code part is as follows:

#include<stdio.h>
int search(int a[], int n, int x)
{
    
    
	int low, mid, high;
	low = 0;
	high = n - 1;
	while (low <= high)
	{
    
    
		mid = (low + high) / 2;
		if (a[mid] == x)
			return mid + 1;
		else if (a[mid] > x)
			high = mid - 1;
		else
			low = mid + 1;
	}
	return 0;
}
int main()
{
    
    
	int i, x, z;
	int a[10];
	printf("请输入10个有序数字,并用分号隔开:");
	for ( i = 0; i <10 ; i++)
	{
    
    
		scanf_s("%d", &a[i]);//为有序数组赋值
	}
	scanf_s("%d", &x);
	z = search(a, 10, x);
	if (z)
		printf("您要查找的数%d在数组中的位置是第%d个元素\n", x, z);
	else
		printf("您要查找的元素%d不在数组中\n", x);
	return 0;
}

3. Advantages and disadvantages of binary search:

Advantages of binary search: the average search length is less than log2n, that is, after one comparison, the search range is reduced by half, and the binary search can be completed after log2n comparisons
. The disadvantage of binary search : because the sequence must be in order, the search sequence must be in order , And arranging all data elements by size is a very troublesome operation. In addition, the insertion and deletion operations of sequential storage structures are inconvenient

Guess you like

Origin blog.csdn.net/weixin_51476766/article/details/111699204