Detailed explanation of binary search method (6 variations)

Preface
In my previous blog, I introduced you to the most basic binary search method ( click me if you haven’t learned it yet! )

Today I will teach you how to use the six deformations of the dichotomy method. Friends, come and start learning today!
Insert image description here

1. Find the first one (from left to right) = target value. If it does not exist, return -1

It is almost the same as the original dichotomy method. When there are repeated target values ​​in an array, this method can be used to find the first subscript from left to right that is equal to the target value.
Because what we are looking for is the first subscript equal to the target value, then we not only look for it on the left when arr[mid] > key, we also have to look for it when arr[mid]>= key, because we need to The leftmost subscript is equal to the target value.
Note:
Finally, we have to determine whether left is out of bounds (left may be equal to the number of array elements), and finally whether arr[left] is equal to the key we are looking for.
Code:

int efcz(int* arr, int key,int left,int right)
{
    
    
 int len = right+1;//数组长度(元素个数)
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] >= key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] < key)
		{
    
    
			left = mid + 1;
		}
	}
	if (left < len&&arr[left] != key)//判断一下是否找到元素
		return -1;
	else
		return left;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,5,6 };//创建数组
	int key = 5;//目标值为5
	int left = 0;//设置左右起点
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key,left,right));//进入二分查找函数
	return 0;
}

2. Find the first one >= target value

This time we need to find the first subscript that is greater than or equal to the target value. This time we do not need to judge whether left is out of bounds. If it is out of bounds, it means that it is not found, which means that the entire array is smaller than the target value.
Code:

//思路和第一种一样,我就不加注释了,如果不懂可以私信问我
int efcz(int* arr, int key, int left, int right)
{
    
    
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] >= key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] < key)
		{
    
    
			left = mid + 1;
		}
	}
		return left;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,6,8 };
	int key = 7;
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key, left, right));
	return 0;
}

3. Find the first > target value

This time we need to find the first subscript that is greater than the target value. This time we also do not need to judge whether left is out of bounds. If it is out of bounds, it means that it is not found, which means that the entire array is smaller than the target value.
In addition, we need to change the judgment conditions inside the function. When arr[mid] <= key, left = mid + 1
because we are not looking for equality, but are looking for something greater than the target value.
Code:

//思路和第一种一样,我就不加注释了,如果不懂可以私信问我
int efcz(int* arr, int key, int left, int right)
{
    
    
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] > key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] <= key)
		{
    
    
			left = mid + 1;
		}
	}
	return left;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,6,8 };
	int key = 5;
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key, left, right));
	return 0;
}

4. Find the last one = target value, if it does not exist, return - 1

Because what we are looking for is the first subscript equal to the target value, then we not only go to the right when arr[mid] < key, but we also have to find when arr[mid]>= key, because we need to The rightmost edge is equal to the subscript of the target value.
Note:
Finally, we have to determine whether right is out of bounds (right may be equal to -1), and finally whether arr[right] is equal to the key we are looking for.
Code:

//思路和第一种一样,我就不加注释了,如果不懂可以私信问我
int efcz(int* arr, int key, int left, int right)
{
    
    
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] > key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] <= key)
		{
    
    
			left = mid + 1;
		}
	}
	if (right >= 0 && arr[right] == key)//判断是否找到
		return right;
	else
		return -1;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,6,8 };
	int key = 5;
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key, left, right));
	return 0;
}

5. Find the last one <= target value

This time we need to find the first subscript that is less than or equal to the target value. This time we do not need to judge that right is out of bounds. If it is out of bounds, it means that it is not found, which means that the entire array is larger than the target value.
Code:

//思路和第一种一样,我就不加注释了,如果不懂可以私信问我
int efcz(int* arr, int key, int left, int right)
{
    
    
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] > key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] <= key)
		{
    
    
			left = mid + 1;
		}
	}
		return right;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,6,8 };
	int key = 4;
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key, left, right));
	return 0;
}

6. Find the last one < target value

This time we need to find the first subscript that is smaller than the target value. This time we also do not need to judge that right is out of bounds. If it is out of bounds, it means that it is not found, which means that the entire array is larger than the target value.
In addition, we also need to change the judgment conditions inside the function. When arr[mid] >= key, right = mid - 1, because we are not looking for equality, but less than the target value.
Code:

//思路和第一种一样,我就不加注释了,如果不懂可以私信问我
int efcz(int* arr, int key, int left, int right)
{
    
    
	int mid = (left + right) / 2;
	while (left <= right)
	{
    
    
		mid = (left + right) / 2;
		if (arr[mid] >= key)
		{
    
    
			right = mid - 1;
		}
		if (arr[mid] < key)
		{
    
    
			left = mid + 1;
		}
	}
	return right;
}
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,5,5,5,5,5,6,8 };
	int key = 5;
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]);
	printf("%d", efcz(arr, key, left, right));
	return 0;
}

Summarize

I think these six deformations can be memorized in two groups. The first three groups are in one category, and the last three groups are in one category. The first three groups all return to left, and the last three groups all return to right. At the same time, we will find that the first and third groups return to left. Four types, the second and fifth, the third and the sixth are very similar, so you can master it by practicing, and it is not easy to forget. This is the end of this issue’s sharing. If you think the blogger’s talk is good If so, don’t forget to give the blogger a follow, like, and favorite~, friends, see you in the next issue!

Supongo que te gusta

Origin blog.csdn.net/Tokai___Teio/article/details/135150125
Recomendado
Clasificación