Introduction to Algorithms 2.3-52 points find

Source: Internet
Author: User

1, two-point search (binary search)

binary Search, also known as binary lookup, is a highly efficient method of finding.
Binary Lookup requirements: The linear table is an ordered table, that is, the nodes in the table are ordered by the keyword, and the storage structure of the table is the sequential structure. It may be advisable to set an orderly table in ascending order.



2, the basic idea of two-point search

Binary Search Algorithm idea:

(1) First determine the midpoint position of the interval: Mid = (left + right)/2;

( 2) Then compare the K value of the unknown origin with the R[mid].key, if it is equal, find the successful and return to this position, otherwise you need to determine the new search interval, continue the binary search, the method is as follows:

① if r[mid].key>k, the order of the table is known r[mid. N].keys are greater than k, so if there is a node in the table where the key is equal to K, the node must be in the sub-table r[1..mid-1] on the left side of the position mid, so the new search interval is left dial hand table r[1..mid-1].

② Similarly, if r[mid].key<k, the K to be found must be in the right child table in mid R[MID+1..N], that is, the new find interval is the right child table R[MID+1..N]. The next lookup is for the new search interval. Thus, starting with the initial lookup interval R[1..N], each time a comparison is made with the node keyword at the midpoint of the current lookup interval, the search is successful and unsuccessful, and the current search interval is reduced by half. This process repeats until a node with the keyword K is found, or until the current lookup interval is empty (that is, the lookup fails).


3, two-point search algorithm code

Iterative implementations:

intBinary_search (intA[], ElementType key,intPintr) {intleft =p; intright=R;  while(Left <=Right ) {          intMiddle = left + ((right-left) >>1); if(Array[middle] >key) right= Middle-1; Else        if(Array[middle] <key) left= Middle +1; Else              returnMiddle; }    return-1; }  

Recursive implementations:

intBinary_search (intA[], ElementType key,intPintR) {    intmid; if(P <=r) {Mid= P + ((R-Q) >>1);if(Key <A[mid])returnBinary_search (A, Key, p, mid-1 ); Else        if(Key >A[mid])returnBinary_search (A, key, Mid +1, R); Else            returnmid; }    return-1;}

Note:

1, two points of the realization of the difficulty is mainly in the boundary of the decision, in the above algorithm, the realization of the array interval A[P....R], recursion and iteration of the end of the conditions need attention.

2, pay attention to overflow problem.

3. If the array contains duplicate elements, one of the subscripts will be returned when the duplicate element is found, not sure which one.

Iv. Analysis of Time complexity

Binary search for the division method, the time complexity of O (LOGN)

Introduction to Algorithms 2.3-52 points find

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.