What is Binary Search? After reading this article, you will understand

foreword

The article uses a simple case of binary search algorithm to explain to you what binary search is, including the specific idea, actual case and summary of binary search algorithm. If you think this article is helpful to you, you can like me who can’t write code. If you have any questions, please leave a message in the comment area, and I will reply to you one by one. Without further ado, let's understand what a binary search algorithm is.

(1) What is binary search algorithm?

Binary search, also known as binary search, binary search, binary search, etc., is a search algorithm designed on the basis of the divide and conquer algorithm. The core idea of ​​the binary search algorithm for searching target elements is to continuously narrow the search area, reduce the difficulty of finding target elements, and then find out the elements we want.
The specific implementation process is as follows:
1. Define an ordered array arr[ ] = { 1,2,3,4,5,6,7,8,9,10 }, the target element is 7
insert image description here
2. According to the subscript, determine the middle The subscript of the element is ( 0+9 )/ 2= 4,
insert image description here
the middle element 5 < 7, it can be concluded that there is no target element in the [0, 4] area, and the target element can only be located in the [5, 9] area, as shown in the figure :
insert image description here
3. In the [5, 9] area, re-determine the subscript of the middle element: [ (4+1) + 9 ] = 7, as shown in the figure: the
insert image description here
middle element 8 > 7, it can be concluded that [7, 9] There is no target element in the area, and the target element can only be located in [5,6], as shown in the figure:
insert image description here
4. In the [5, 6] area, the subscript of the middle element is: [ 5 + (7-1) ] = 5, the middle element 6<7, there are only two elements in the range you are looking for and the middle element is not the target element you are looking for, that is, the target element you are looking for is 7.

Note: The binary search algorithm is only applicable to ordered sequences, and it can only be used to find target elements in ascending or descending sequences.

(2) Simple case of binary search algorithm

Find a specific number n in an ordered array, define an ordered array arr[ ] = { 1,2,3,4,5,6,7,8,9,10 }, assuming the number you are looking for k=7.
1. Use the sizeof operator to calculate the number of elements in the array, that is, the number of elements in the array = the length of the array/the size of an element in the array
2. Find the left and right subscripts of the array elements
3. Define the while dichotomy search loop Body
The specific steps have been given relevant comments in the code, the code is as follows:

#include <stdio.h>
int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int k = 7;
	int sz = sizeof(arr) / sizeof(arr[0]);//计算元素个数
	int left = 0;//左下标
	int right = sz - 1;//右下标=元素个数-1
	//二分查找循环体
	while (left<=right)//在查找过程中,如果要查找的元素不存在,就会出现左下标和右下标交错的情况,所以while循环应满足左下标小于或等于右下标的条件
	{
    
    
		int mid = (left + right) / 2;//中间元素下标
		if (arr[mid] > k)//如果要找的元素小于中间元素的下标,则新的右下标=mid-1,左下标不变
		{
    
    
			right = mid - 1;
		}
		else if (arr[mid] < k)//如果要找的元素大于中间元素的下标,则新的左下标=mid+1,右下标不变
		{
    
    
			left = mid + 1;
		}
		else
		{
    
    
			printf("找到了,下标是:%d\n", mid);//要找的元素正好是中间元素
			break; //跳出循环
		}
	}
	if (left > right)//不满足左下标小于或等于右下标的条件,则要找的元素不存在
	{
    
    
		printf("找不到\n");
	}
	return 0;
}

(3) Summary

1. For the code, only when left<=right (left subscript <= right subscript), it will enter the while loop to search, otherwise the element to be searched will not be in the ordered array 2. During each
search , the subscript of the middle element must be calculated according to the left and right subscripts, how to compare the subscript of the middle element with the searched element 3. Satisfy the
left<=right condition to enter the loop, and finally break out of the loop
4. No Satisfies the left<=right condition, that is, the searched element cannot be found, and the program code is divided into two cases: found and not found

Guess you like

Origin blog.csdn.net/qq_52730883/article/details/129637650