"Classic algorithm-find" binary search

Source: Internet
Author: User

Binary lookup, also known as binary lookup, applies only to sequential tables that have been ordered in advance. The basic idea of its finding: first, compare the given value K with the key of the middle position element in the table, and if it is equal, return the storage location of the element; if not, the element to be searched can only be in the first half or the second half of the intermediate data. Then continue with the same search in the narrowed range. So repeat until you find it. The algorithm is as follows:

  

1 template<typename t> 2 int binarysearch (vector<t> &data, T key) {3     int low = 0, high = data.size () -1; 4     while (low <= high) {5         int mid = low + (high-low)/2; 6         if (data[mid] = = key) {7             return mid; 8
   } else if (Data[mid] > key) {9 High             = mid-1;10         } else {one low             = mid + 1;12         }13     }14     ETURN-1;16}

  Because the binary lookup needs to conveniently locate the lookup area, the storage structure that is suitable for binary lookups must have a randomly stored feature. Therefore, the lookup method is only suitable for the sequential storage structure of linear tables, is not suitable for chained storage structures, and requires elements to be ordered in an orderly manner by keyword.

Decision Tree:

  The binary lookup process can be expressed as a decision tree. Each circular node in the tree represents a record, and the value in the node is represented as a key value for the record: the bottom-most leaf node in the tree is square, which indicates that the lookup was unsuccessful. From the decision tree, it can be seen that the lookup is successful when the lookup length is the number of nodes on the path from the root node to the destination node, while the lookup is unsuccessful when the lookup length is the number of nodes from the root node to the parent node of the corresponding failed node; If there are n elements in an ordered sequence, the corresponding decision tree has n circular non-leaf nodes and n+1 square leaf nodes.

  

, the depth of a tree consisting of n circular nodes (representing an ordered sequence of n elements) is equal to the depth (height) of the full binary tree of n nodes, both ⌊log2n⌋+1 or ⌈log2 (n+1) ⌉

The time complexity of binary lookup is O (log2n), which is more efficient than sequential lookup.

From the above analysis, it is found that a binary lookup to a given value or a lookup failure is no more than the height of the tree. Finding success and not succeeding, in the worst case, you need to compare ⌊log2n⌋+1 times.


Advantages and disadvantages of two-point search
Although binary lookups are efficient, you want to sort the tables by keyword. The sort itself is a time-consuming operation. It also takes an O (NLGN) time to use an efficient sorting method.
Binary lookup applies only to sequential storage structures. To maintain the order of the table, it is necessary to move a large number of nodes to insert and delete in the sequential structure. As a result, binary search is especially useful for linear tables that are rarely modified and often need to be found once created.
For those linear tables that find little and often need to be changed, a chain list can be used as a storage structure for sequential lookups. Binary lookups cannot be implemented on a linked list.

Related Topics:

1. http://www.cnblogs.com/vincently/p/4122528.html

2. http://www.cnblogs.com/vincently/p/4122676.html

3. "Sword Point offer" face question 8

Related information:

1.http://www.zhihu.com/question/22422613

2.http://www.cppblog.com/converse/archive/2009/10/05/97905.html

3.http://community.topcoder.com/tc?module=static&d1=tutorials&d2=binarysearch

4.http://www.cnblogs.com/bhlsheji/p/4211826.html

Resources:

  1. "Benevolent Programmer"

2. http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.2.2.1.htm

  

"Classic algorithm-find" binary search

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.