A summary of binary search for the beauty of Programming

Source: Internet
Author: User

The binary search principle is very simple, but the boundary conditions are prone to errors and endless loops. to thoroughly distinguish them, you must understand them thoroughly. You can first think about them and then use examples to verify them, the following is my idea. If there is any error, please correct it. Let's first look at deformation 1: if there are a lot of numbers to be searched, find the largest ones, so the biggest one must be at the rightmost. In order to compare the two numbers between the left and right, at the end of the loop, the numbers of the left and right pointers must be separated by 1, so the condition for loop end is left <right-1; to retain the maximum subscript, when data [Mid] = value, it cannot end, because data [Mid + 1] may also be equal to value, we need to continue traversing. In this case, Left = mid, so less than and equal to merge. Deformation 2 is similar. Let's look at deformation 3: To find the maximum I of data [I] <value, data [I] = value cannot end. At this time, because I must be on the left of the lower mark equal to value, when data [I] = value, we need to traverse right = mid, that is, merge the greater than and equal to values, in this case, if it is different from deformation, the value is smaller than, rather than equal to, so when data [I]> = value, the result must be on the left of I, and cannot include I, so right = mid-1 instead of mid, deformation four is similar.

Standard binary lookup 1:

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size ()-1; // Note 1 while (left <= right) // Note 2 {int mid = left + (right-left)> 1 ); // note 3if (data [Mid] <value) Left = Mid + 1; else if (data [Mid]> value) Right = mid-1; // Note 4 else return mid;} return-1 ;}

Standard binary lookup 2 (not commonly used ):

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size (); // Note 1 while (left <right) // Note 2 {int mid = left + (right-left)> 1 ); if (data [Mid] <value) Left = Mid + 1; else if (data [Mid]> value) Right = mid; // Note 3 else return mid ;} return-1 ;}

Deformation 1: If multiple conditions are met, the maximum number

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size ()-1; // note that 1 while (left <right-1) {int mid = left + (right-left)> 1 ); if (data [Mid] <= value) Left = mid; // Note 2 else right = mid; // Note 3} If (data [right] = value) return right; if (data [left] = value) return left; Return-1 ;}

Deformation 2: If multiple conditions are met, the minimum sequence number is returned.

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size ()-1; // note that 1 while (left <right-1) {int mid = left + (right-left)> 1 ); if (data [Mid]> = value) Right = mid; // Note 2 else left = mid; // Note 3} If (data [left] = value) return left; If (data [right] = value) return right; Return-1 ;}

Deformation 3: Find the largest I so that data [I] <Value

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size ()-1; // note that 1 while (left <right-1) {int mid = left + (right-left)> 1 ); if (data [Mid] <value) Left = mid; // Note 2 else right = mid-1; // Note 3} If (data [right] <value) return right; if (data [left] <value) return left; Return-1 ;}


Deformation 4: Find the smallest I so that data [I]> Value

Int bisearch (vector <int> & Data, int value) {int left = 0, Right = data. size ()-1; // note that 1 while (left <right-1) {int mid = left + (right-left)> 1 ); if (data [Mid]> value) Right = mid; // Note 2 else left = Mid + 1; // Note 3} If (data [left]> value) return left; If (data [right]> value) return right; Return-1 ;}

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.