The study of dichotomy method

Source: Internet
Author: User

1. Find the Dichotomy method

In general, the dichotomy is in the array, and the inside of the array is already sorted from large to small or small to large, and we need to find the values we need in these sorts.

intBinart (int*a,intKeyintN) {    intleft =0, right = N-1, Mid =0; Mid= (left + right)/2;  while((left<right) && a[mid]!=key) {        if(a[mid]<key) { Left= Mid +1; }        Else if(A[mid] >key) { Right= Mid-1; } Mid= (left + right)/2; }    if(A[mid] = =key) {        returnmid; }    return-1;}

2. Number of binary search methods

The dichotomy is found several times, the range is F, and the number of steps to find is S, then, S = log2 (f) + 1; The log here is low at 2. So when the range is 100, the number of lookups is 7, and when the range is 1000, the number of lookups is 10.

The study of dichotomy method

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: 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.