Binary Search Learning

Source: Internet
Author: User

An array searches for an element in an ordered manner. Binary Search can increase the time complexity of logn. Although binary search seems easy, it is not easy to write this algorithm and use it flexibly. It is said that 90% of professional programmers cannot achieve it correctly. If you don't believe it, try it yourself and test it with some test cases.

The idea of Binary Search: Search for X in the ordered array a [n], so that S = 0, E = n-1. We first believe that X is in the range of [s, E, M = (S + E)/2 is the Center Coordinate. If x = A [m] is the right coordinate, the output is found. If x <A [m] is used, X appears on the left of M, on the Right of M. In this way, the search range can be reduced. If S> EIS not found,-1 is returned.

It is not difficult to write the following code based on your ideas:

int binarySearch(int A[],int n,int x){    int s = 0;    int e = n-1;    while(s<=e)    {        int mid = (s+e)>>1;        if(A[mid]<x)            s = mid + 1;        else if(A[mid]>x)            e = mid - 1;        else            return mid;    }    return -1;}

Note: If the above Code is worried about S + e overflow, you can use mid = S + (E-S)> 1;

The above implementation is easy and error-free. Be careful when using several other variants.

Variant 1:

int binarySearch1(int A[],int n,int x){    int s = 0;    int e = n;    while(s<e)    {        int mid = (s+e)>>1;        if(A[mid]<x)            s = mid + 1;        else if(A[mid]>x)            e = mid;        else            return mid;    }    return -1;}

This method uses the boundary (not included) of the open range on the right ). Note that when a [Mid]> X is used to modify the end coordinate e, the right boundary is also not included, that is, E = mid.

Variant 2:

If the array contains multiple elements that meet the conditions, we randomly find a location in algorithm 1. In programming Pearl, a method is provided to find the first location that meets the conditions.

// Find the first element that meets the condition, xint getfirst (int A [], int N, int X) {int S =-1; int e = N; while (S + 1! = E) // S <E & A [s] <x <= A [e] {int mid = (S + E)> 1; if (A [Mid] <X) S = mid; else E = mid;} If (E> = n | A [e]! = X) E =-1; Return e ;}

This method may not be easy to understand. At least when I first read it, I wondered why I should write it like this (No way, my head turns slowly! -!). After thinking about it, I finally figured it out: because we are looking for the first satisfied person, we should narrow down the scope of a certain judgment, whether or not we are looking for it or not,The key is to find "it" and what part of it is better?It must be the right half, because it turns into a locomotive.

Similarly, we can find the method for finding the last position:

// Locate the last element that meets the condition at the X position int getlast (int A [], int N, int X) {int S =-1; int e = N; while (S + 1! = E) // S <E & A [s] <= x <A [e] {int mid = (S + E)> 1; if (A [Mid]> X) E = mid; else S = mid;} If (S <= 0 | A [s]! = X) S =-1; return s ;}

In addition, there are other variants, such as finding the first position greater than the given element, finding the first position smaller than the given element, and finding the first position greater than or equal to the given element. They all look similar, it's not that easy to write. I will write it here today, and I will add some more later. We also welcome comments and comments from our friends ~

 

Binary Search Learning

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.