Detailed explanation of the binary search method and the half search method in [C language] (example problem solution)

question

Find a specific number n in an ordered array

For example, I bought a pair of shoes, and you asked me how much it cost, and I said no more than 300 yuan. You are still curious, how much do you want to know, I will let you guess, how would you guess?

Answer: You guess the middle number every time

ideas

We first define a set of ordered arrays, assuming:int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

Since we know that the index of the array starts from 0 , let's say I want to find the index of the number 7

Look at a picture:
insert image description here
the subscript of the number 1 is 0 , and the subscript of the number 10 is 9

We first find the middle element: (0+9) / 2 = 4(note: here \is no remainder), then we get the number 5 with the subscript 4

And the number 5 is smaller than the 7 we are looking for, indicating that we cannot find it on the left side of the number 5.

So our search is narrowed down to: 数字6~10, is our range reduced by half?

So how do we find this new range?

In fact, it is very simple, as shown in the figure , this is the new range, the right subscript is still 9 , and the left subscript becomes: 4+1=5

insert image description here
The left subscript 5 corresponds to the number 6, and the right subscript 9 corresponds to the number 10

I still find the middle element first: (5+9) / 2 = 7, then if 7 is used as a subscript, the corresponding number is 8
insert image description here
and the number 8 is larger than the number 7 I am looking for , indicating that the subscript we are looking for is to the left of the number 8

So the scope of our search is reduced to: 数字6~7, then is my search scope reduced by half again?

Then we can get a new range: as shown in the figure, the left subscript is still 5 , and the right subscript becomes: 7-1=6
insert image description here
At this time , the left subscript 5 corresponds to the number 6, and the right subscript 6 corresponds to the number 6. The number 7, then the middle element is:(5+6) / 2 = 5

Here we get the middle element as 5, and the locked number is 6

The number 6 is smaller than the number 7 I am looking for , which means that the element I am looking for is on the right side of the number 6, and on the right side of the number 6, there is only one number 7 left in the search range.

Then the left subscript of the number 7 is 6 , and the right subscript is still 6

Then I still use the subscript calculation method: (6+6) / 2 = 6, then the element locked by 6 is our number 7. The
insert image description here
number 7 is compared with the 7 we are looking for, and they are equal! Then the description has been found

This is the process of binary search!

Detailed explanation

{ 1,2,3,4,5,6,7,8,9,10 }During the process I just searched, you will find that the middle element I am looking for every time

The middle element is smaller than the element I am looking for, indicating that the element I want to find is to the right of the middle element, then the range is reduced by half

In this way of thinking, the scope is reduced by half every time, and the search is reduced by half

This algorithm is called: halved search or binary search

code

in the code below

int main()
{
    
    
    int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };

    //计算数组元素的方法:
    int sz = sizeof(arr) / sizeof(arr[0]);
    //sizeof(arr)计算的是数组的总大小,单位是字节,这组数组是10个元素,每个元素是int类型
    //所以总大小为40
    //sizeof(arr[0])就是计算第一个元素的大小,第一个元素的大小为1个int,也就是4个字节
    //40 / 4 = 10
    
    int k = 7; //我们要查找的数字
    
    int left = 0;//定义左下标
    
    int right =  sz - 1; //(元素 - 1)就为右下标

    while (left <= right)
    {
    
    
        int mid = (left + right) / 2; //mid定义中间元素
        if (arr[mid] < k)
        {
    
    
            left = mid + 1;
        } 
        else if (arr[mid] > k)
        {
    
    
            right = mid - 1;
        }
        else
        {
    
    
            printf("找到了,下标是:%d\n", mid);
            break; //找到了就停下来
        }    
    }
    //当左下标>右下标时,是找不到的
    if (left > right)
    {
    
    
        printf("找不到\n");
    }
    return 0;
}

Result of code execution:
insert image description here

Guess you like

Origin blog.csdn.net/m0_63325890/article/details/120993495