Find--order lookup vs Binary Lookup

Source: Internet
Author: User

The search based on linear structure is mainly the order finding and binary finding 1. Sequential Lookup

The feature is to compare the keywords given to each element of the linear table one by one until it succeeds or fails.

The basic idea is to set up an additional unit called a "lookout" at one end of the lookup table and hold the data element keyword to find. The lookup is then started from the other end of the table, and if a given keyword is found at the "Sentinel" location, it fails, otherwise the position of the corresponding element is successfully returned 2. Binary Find

Also known as two-point search .
This lookup method requires the unknown origin table to meet two conditions: first, the lookup table must use the sequential storage structure. Second, the lookup table must be sorted by keyword size.

The basic idea is:
First, compare the keywords of the lookup table's intermediate position data elements with the given keywords, and if they are equal, the search succeeds;

--otherwise use the intermediate element to split the table in half, and if the middle element keyword is larger than the given keyword, the binary lookup is made in the previous child table;

--Otherwise, the binary lookup is performed in the latter child table.

--Repeat the above procedure to find the element that satisfies the condition, or find out if the child table is empty, and the lookup is unsuccessful at this time.

Example 8-1 is known as an ordered table of 11 data elements, and finds keywords 27 and 28 in an ordered table.
(5,13,17,27,36,41,46,50,75,88,92)


/**
 * 
 * Binary Lookup--Non-recursive form */public
int binsearch (int[] s,int low,int high,int key) {When
    (low <= high ) {
        int mid = (low + high)/2;
        if (s[mid] = = key) {
            return mid;
        } else if (S[mid] > key) {high
            = mid-1;
        } else{low
            = mid+1;
        }
    }
    return-1;
}
/**
 * 
 * Binary Lookup--recursive form */public
int binsearch (int[] s,int low,int high,int key) {
    int mid = (low + High)/2 + low;
    if (s[mid] = = key) {
        return mid;
    }
    if (low >=) {
        return-1;
    } else if (S[mid] > key) {high
        = mid-1;
        Return Binsearch (S,low,high,key);
    } else if (S[mid] < key) {low
        = mid+1;
        Return Binsearch (S,low,high,key);
    }
    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.