First acquainted with array search (linear search and binary search)

Let’s first understand what lookup is.
Uh, well, there’s nothing to understand,
but to find the position or existence of an element in the array.
That's it, no more. Learn about the search algorithm directly.

Linear search

Linear search is somewhat different from binary search.
The elements in the array can be disordered, that is, they are not stored in order. This method is very simple, starting from the first element, searching backwards and comparing. That's it. Use loops and compare in turn.
Look at the code.

#include <stdio.h>
int main(void)
{
    
    
	int arr[] = {
    
     5,4,6,8,7,9,10,2,3,1 };
	int len = sizeof(arr) / sizeof(arr[0]);//计算数组的元素个数
	int i;
	int n;
	scanf("%d", &n);//输入要查找的元素

	for (i = 0; i < len; i++)
	{
    
    
		if (arr[i] == n)
		{
    
    
			printf("%d的下标是%d\n", n, i);
			break;//找到后就直接跳出循环
		}
	}
	
	if (i == len)//因为如果数组元素全部遍历一遍后,都没有i++等于len后,便跳出循环再判断说不存在。
		printf("Don't have number %d\n", n);
	
	return 0;

}

Linear search is very simple, but if the array elements are large, it is more troublesome. After all, the complexity of traversing the past time is n.

Binary search

Let's take a look at the binary search, which is the dichotomy that I have learned in high school mathematics. The principle is quite simple. But it can only search the sorted array, which has some limitations compared with linear search.
By comparing the size of the intermediate data of the array and the target data, it is judged whether it is on the left or the right of the intermediate data, which instantly reduces the amount of calculation by half. Then continue to compare in this way until it is found or not found.

#include <stdio.h>
int main(void)
{
    
    
	int n;
	scanf("%d", &n);
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10};
	int len = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = len - 1;
	int mid;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] > n)
		{
    
    
			right = mid-1;
		}
		else if (arr[mid] < n)
		{
    
    
			left = mid+1;
		}
		else 
		{
    
    
			break;
		}
	}
	if (left <= right)
		printf("%d的下标是%d\n", n, mid);
	else 
		printf("DON't have number %d\n", n);
	return 0;

}
	/*int i = 1;

Look at the picture for easy understanding and memory.
Insert picture description here
Look at the code, the middle element is 5, which is to the right of 5, and then remove the unnecessary elements out of the comparison range, and then reset the middle element for comparison.
Insert picture description here
Take another 8 for comparison, which is to the left of 8. Re-plan the scope.
Insert picture description here
7 is greater than 6, then it is on the right of 6, continue to compare.
Insert picture description here
At this time, left==right, according to the while loop condition, you can still enter the loop, but arr[mid]7. It means that the element has been found, it will break; jump out of the loop, and then judge that the condition satisfies left<=right, which means that it still holds, and then output.
Otherwise, if the target element is 11, it will always be the right side of the middle element.
Insert picture description here
Then left=MID+1 is 10, at this time, left
right, the loop is not over yet, this time, mid is equal to 10 or less than 11, left=10+1, and at this time, left>right, if the condition is not met, the loop ends, and then judge, if the condition is not met, enter else, , Which means that 11 is not in the array.
When I wrote binary search for the first time, I didn’t write

left = mid+1;
right = mid-1;

But write

right = mid;
left = mid;

I thought it was almost the same, uh, in fact it is almost the same, but when the target data is not in the array, judge in advance. If you change the conditions directly in the form of the above code, it will cause left to always be 9, right to always be 10, and mid to always be 9, which can't break out of the loop, resulting in such an endless loop.
Be sure to remember when writing binary search.
These two inspections, that's all for now.
If you have any questions, please give me some advice.
Thanks for watching.

Guess you like

Origin blog.csdn.net/weixin_52199109/article/details/112726928