Efficient algorithm -- binary search

Keywords: C++

Today I got a lot of new knowledge. share it with you.
Binary search
For example, when I bought a pair of shoes, you asked me how much it was. I said it was no more than 300 yuan. You're still curious. If you want to know how much, I'll let you guess. How would you guess?
Answer: you guess the middle number every time.
Right, so we usually guess the numbers from the middle, and then adjust them according to the big or small ones. In fact, it saves a lot of trouble when applied to the algorithm, directly reduces the search range by nearly half, and greatly improves the operation efficiency. As this algorithm is very efficient, the following is for you to understand:

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

    int left = 0;
    int right = sizeof(arr)/sizeof(arr[0])- 1;
    int mid = 0; 

    while (left <= right)
    {
        mid = left + (right - left) / 2;
        if (arr[mid] < k)
        {
            left = mid + 1;
        }   
        else if (arr[mid]>k)
        {
            right = mid - 1;
        }
        else
        {
            printf("Found. The subscript is:%d\n",mid);
            break;
        }
    }
    if (left > right)
        printf("Can not find\n");
    system("pause");
    return 0;
}

This is a simple way to find array elements. Of course, Xiao Luo will continue to expand in the future. For example, if you input an array, find the subscript of the element you just entered again, and code optimization will be carried out again. Of course, we can also set it as a function, which is more convenient for us to call. The following is the functional model:

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

    while (left <= right)
    {
        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;
}

If we find the subscript of this element, we will directly return this subscript, otherwise - 1 will be returned. This makes the code more concise and concise. Attach the main function:

int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int k = 7;
    int sz = sizeof(arr) / sizeof(arr[0]) - 1;
    int ret = binary_search(arr,k ,sz);
    if (ret == -1)
    {
        printf("I can't find it.!\n");
    }
    else
    {
        printf("Found. The subscript is:%d", ret);
    }

    system("pause");
    return 0;

}

Let's do some more optimization to make the code more flexible. Such as:

int binary_search(int arr[], int k, int left,int right)
{
    int mid = 0;

    while (left <= right)
    {
        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;
}

In this way, we can modify left and right, and improve efficiency. Is it very useful?
Next time Xiao Luo will bring more knowledge and grow with you.

Posted by sonofyoda on Mon, 25 Nov 2019 09:55:06 -0800