C language binary search

Table of contents

1. Binary search algorithm

Second, the small points that should be paid attention to in the branch statement


 

1. Binary search algorithm

The so-called binary search is to find whether a given number is in this sequence in a set of ordered sequences .

There are three main steps:

1. Determine the left and right subscripts left
and right of the range being searched 2. Determine the subscript mid of the middle element according to left and right
3. Determine the new search range left and right according to the comparison between the mid-locked element and the searched element

 The following three steps will be explained with diagrams and codes:

1. Assume that the number of elements in the given array is an odd number

2. Assume that the given array is an even number

 3. Assume that the given number is not in this sequence

 According to the above three situations, the code can be written as follows:

#include <stdio.h>
int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
    int left = 0, right = sizeof(arr) / sizeof(arr[0]) - 1;
    int x = 0,flag = 0;

    scanf("%d", &x);//要找的数

    while (left <= right)//若要找的数在此数组中,此条件会一直成立;
                         //若要找的数不在此数组中,最终left会大于right,从循环中跳出
    {
        int mid = (left + right) / 2;
        if (x == arr[mid])
        {
            printf("%d\n", mid);
            flag = 1;
            break;
        }
        else if (x > arr[mid])
        {
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }
    if (flag == 0)//只有当要找的数在数组中找不到时flag == 0
    {
        printf("找不到\n");
    }
    return 0;
}

 Summary: As can be seen from the above example, the dichotomy solution is a very efficient method, because half of the possibilities can be ruled out at one time. But also note that the dichotomy only applies to ordered sequences

Second, the small points that should be paid attention to in the branch statement

1. Dangling else statement

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 2;
	if (a == 1)
		if (b == 2)
			printf("hehe\n");
		else
			printf("haha\n");
	return 0;
}

In the code above, someone might misunderstand which if statement is paired with the else statement.

In fact: else matches the nearest if. But if it is written like the above, it is easy to cause ambiguity. can be written in the following form:

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 2;
	if (a == 1)
	{
		if (b == 2)
		{
			printf("hehe\n");
		}
	}
	else
	{
		printf("haha\n");
	}
	return 0;
}

Appropriate use of {} can make the logic of the code clearer.

2. The break in the switch statement

switch allows nested use

#include <stdio.h>
int main()
{
	int n = 1;
	int m = 2;
	switch (n)
	{
	case 1:
		m++;//m == 3
	case 2:
		n++;//n == 2
	case 3:
		switch (n)
		{//switch允许嵌套使用
		case 1:
			n++;
		case 2:
			m++;//m == 4
			n++;//n == 3
			break;
		}
	case 4:
		m++;//m == 5, n == 3
		break;
	default:
		break;
	}
	printf("m = %d, n = %d\n", m, n);
	return 0;
}

In the above code, some case statements are not followed by a break, which will cause a case statement below it to be executed after a case statement without a break is executed, which may lead to the judgment output result we want different. Because switch more often performs the function of conditional judgment, it is best

Add a break after every valid case statement. Also note that it is good practice to put a default clause in every switch statement, and you can even add a break after it.

Guess you like

Origin blog.csdn.net/m0_74265792/article/details/130301993