Understanding of dichotomy and pairing programming

Source: Internet
Author: User

First, the experience of the idea of the dichotomy

1. The two-cent method is a typical example of the use of the divide-and-conquer strategy, also known as binary, which takes full advantage of the order relationship between elements and is a highly efficient method of finding. Two methods are recursive and non-recursive to realize the binary algorithm.

2. Basic idea: divide n elements into roughly the same two halves, take A[N/2] and compare with X. If X=A[N/2], the x is found, the algorithm terminates, and if A<[N/2], only the left half of the array a continues to search for x, and if X>A[N/2], only the right half of the array continues to search for X.

3. Code implementation:

intBinarySearch (Type a[],ConstType&x,intN) {    intleft =0;intright = N1;  while(left<=Right ) {    intMiddle = (left + right)/2; if(X==a[middle])returnMiddle; if(x >= a[middle]) left = middle +1; Elseright = Middle-1; }  return-1;}

4. Analysis of Time complexity:

In this process, the size of the array to be searched is reduced by half for each time the while loop is executed. Therefore, in the worst case, the while loop is executed O (Logn) times. The loop in vivo operation requires O (1) time, so the computational time complexity of the entire algorithm in the worst case is O (logn).

5. Advantages: Improve the search efficiency, reduce the complexity of time, easy to achieve

Cons: With certain limitations, only applicable to sequential arrays

Second, about pairing programming

Pairing programming makes it easier for us to find our own mistakes in programming, so-called "lookers, Onlooker sees most," and sometimes the code you write is not easy to find errors. With the partner knot pair process, there are problems two people together to smooth the implementation of the code, quickly find out the errors found therein. Some confusing questions. It is easier to find the answer for two of people to discuss together. Pairing programming also makes us understand some other people's ideas about code, learn a lot of things, improve the efficiency of programming, broaden their own problem-solving thinking.

Understanding of dichotomy and pairing programming

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.