C language uses binary search

Preface

If there is a set of ordered numbers and I want to find one of the numbers, how should we do it? Generally, do we directly use loops to compare one by one? Wouldn't this be too troublesome and too inefficient? So today we are going to learn a new way to solve this problem.

dichotomy

If we are given ten elements 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and I want to find the element 7 among them, can we first find the middle position of these numbers? As for numbers, does this mean that we have reduced the scope by half? So can we find an intermediate value by repeating this operation? Can we find the number we want by comparing the intermediate value with the number we are looking for by repeating this operation? If the number we are looking for is larger than the middle number, then the number will be found to the right of the middle number, so the range is directly reduced to 6, 7, 8, 9, 10, and it can be found quickly with the same operation. Element 7. It seems that this method is not only not that simple, but also quite complicated. That is because the number of these elements is relatively small. If we are given 10,000 elements and let us find one, then everyone will know which method without even thinking about it. Faster and more efficient. Next I will show you the specific operation.

1. Put all the elements into an array, define the subscripts, and use the subscripts to perform binary queries.

int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int left = 0;
	int right = 9;
	int mid;

2. Use the intermediate value to compare the size with the value being searched and then narrow the range.

mid = (left + right) / 2;
	if (arr[mid] > 7)
		rigth = mid - 1;
	else if (arr[mid] < 7)
		left = mid + 1;
	else
		printf("找到了,下标为%d", mid);

3. Because we need to perform multiple searches, we have to add a loop. The loop condition is right>left.

while (left < right)
	{
    
    
	   mid = (left + right) / 2;
		if (arr[mid] > 7)
			right = mid - 1;
		else if (arr[mid] < 7)
			left = mid + 1;
		else
		{
    
    
			printf("找到了,下标为%d", mid);
			break;
		}
	}

Here we have to remember to add break to it when we find this number to break out of the loop.

Next let's look at the complete code.

#include<stdio.h>
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int left = 0;
	int right = 9;
	int mid;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] > 7)
		{
    
    
			right=mid-1;
		}
		else if (arr[mid] < 7)
		{
    
    
			left = mid+1;
		}
		else
		{
    
    
			printf("找到了,下标为%d", mid);
			break;
		}
	}
		return 0;

}

Next let's see the practice questions.

Write a function to implement binary search on an integer sorted array.

int binary_search(int arr[], int k, int sz)
{
    
    
	int left = 0;
	int right = sz - 1;

	while (left<=right)
	{
    
    
		//int mid = (left + right) / 2;
		int mid = left + (right - left) / 2;

		if (arr[mid] < k)
		{
    
    
			left = mid + 1;
		}
		else if (arr[mid] > k)
		{
    
    
			right = mid - 1;
		}
		else
		{
    
    
			return mid;
		}
	}
	return -1;
}

int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%d", &k);//输入的是要查找的值
	//FindNum();
	int sz = sizeof(arr) / sizeof(arr[0]);
	int ret = binary_search(arr, k, sz);
	if (ret == -1)
	{
    
    
		printf("找不到\n");
	}
	else
	{
    
    
		printf("找到了,下标是:%d\n", ret);
	}
	return 0;
}

Here we use the sizeof operator to calculate the length of the array. sizeof calculates the number of bytes. One element of an int variable is four bytes. sizeof(arr)/sizeof(arr[0]) calculates the number of elements. number, so the left subscript is 0, and the right is the length sz just found minus 1 (sz-1).

That’s it for today’s sharing. Thank you all.

Guess you like

Origin blog.csdn.net/Lehjy/article/details/132244421