Implementation of binary search and precautions

Source: Internet
Author: User

When you hear about binary search, you may think it is very simple and will naturally ignore it. So is there anything worth noting in implementing this seemingly simple algorithm?
Below is the implementation of a binary search I wrote.
Int binary_search (int array [], int n, int value)
{
Int begin = 0, end = n-1, mid = 0;
Bool flag = 0; // determines the data sorting method. The value ranges from small to large, and the value ranges from large to small.
For (int I = 0; I <n-1; ++ I) // 1
{
If (array [I] <array [I + 1])
{
Flag = 1;
Break;
}
If (array [I]> array [I + 1])
{
Flag = 0;
Break;
}
}

If (flag)
While (begin <= end) // data sorting method, from small to large
{
Mid = begin/2 + end/2; // 2
If (array [mid] = value)
Return mid;
If (array [mid]> value)
End = mid-1; // 3 Note-1
Else
Begin = mid + 1; // 4 Note + 1
}
Else
While (begin <= end) // data sorting method, from large to small
{
Mid = begin/2 + end/2;
If (array [mid] = value)
Return mid;
If (array [mid]> value)
Begin = mid + 1;
Else
End = mid-1;
}
Return-1;
}


In this program, we need to pay attention to the role of the loop in Note 1. As we know, binary search should be implemented based on the sorted array, in what order is this array arranged? from large to small, it is still small to big. This loop is used. Generally, it will only loop once to twice. Because different sorting methods fail to query, the operations to narrow down the search range are different, as can be seen from the code above. In order for this program to apply to the arrays of the two sorting methods, this judgment is essential.


Let's take a look at the statement in comment 2. Why should mid be equal to begin/2 + end/2 instead of directly (begin + end)/2? There is no difference in mathematics, but there is a big difference in computers. Suppose your array is very large. For convenience, I take 16 bits as an example. The maximum value of an int integer is 65535. If your array size is 40000, the data you want to search for is located at the position after the comparison of the array, for example, the number with the subscript of 39800. Then, in the subsequent search (begin + end) it will go beyond the range expressed by 65535, And the mid will become a negative number, so that you will never find the number you are looking for, and there may be a memory error.


Let's take a look at comments 3 and 4. Many people may miss adding 1 and 1 when narrowing down the search scope. What are the consequences, that is, this program may fall into an endless loop, which is also a common error. But why can we add 1 and minus 1, because when the search fails, the mid will certainly not be equal to the value, so we can rest assured from the next element of the mid (Add 1) or the first element (minus 1) of the mid to start searching.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.