1. Definition
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 vector is used as the storage structure of the table . It may be advisable to set an orderly table in ascending order.
2. Basic Ideas
The basic idea of binary search is:
Set R[low. High] is the current search interval
(1) First determine the midpoint position of the interval:
(2) Then compare the K value of unknown origin with the R[mid].key: if equal, the lookup succeeds and returns 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].
② if r[mid].key<k, the K to be looked up in the right sub-table of mid R[MID+1..N], that is, the new search interval is the right sub-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. Storage structure
Binary lookup applies only to sequential storage structures .
4, two-part search algorithm
/* Binary find */int binary_search (int a*,int n,int key) { int low,high,mid; low=1; /* Define the bottom mark as the first record */ high=n; /* Define highest subscript as record bottom * /while (Low<=high) { mid= (Low+high)/2; /* Binary * /if (Key<a[mid]) high=mid-1; if (Key>a[mid]) low=mid+1; else return mid; } return 0;}
You can also construct parameters as follows:
int Binsearch (seqlist r,keytype K) {///in ordered table R[1..N], the position of the node is returned on success, and 0 int low=1 on failure ; int high=n; int mid; The initial value while (Low<=high) //Current lookup interval R[low in the current lookup interval. High] non-null { mid= (Low+high)/2; if (r[mid].key==k) return mid; //Find successful return if (r[mid].kdy>k ) high=mid-1; Continue in R[low. MID-1] Find else low=mid+1; //Continue to find in R[mid+1..high] } return 0; When Low>high indicates that the lookup interval is empty, the lookup fails}
5. Algorithm Analysis
① Execution Process
An ordered sequence of keywords in an input instance of the algorithm
(05,13,19,21,37,56,64,75,80,88,92)
Expand:
Two-point search decision Tree
The binary search process can be described using a two-fork tree: The nodes in the middle of the current search interval as the root, and the nodes in the Zoozi and right child tables as the Saozi right subtree of the root. The resulting two-fork tree, called the decision Tree (decision tree) or the comparison tree, that describes the binary lookup (Comparison tree).
Attention:
The shape of the decision tree is related only to the number of table nodes n, but not to the value of R[1..n].keys in the input instance.
(1) The composition of two-point search decision Tree
① The Circle node is the inner node in the tree. The number within the Circle node in the tree represents the position of the node in the ordered table.
② external nodes: all null pointers in a round node are replaced by a virtual square node, which is an external node.
The marks "<", "(", ">", ")" on the Left (right) branch of a node I and its left (right) child in the ③ tree indicate: When unknown origin keyword K<r[i].key (k>r[i].key), the left (right) branch should go to the left (right) child of I, Compare the child's keywords further to the K. If it is equal, the lookup process ends with a return, otherwise the k is compared to the next-level node in the tree.
(2) Finding a decision tree with two points
Binary lookup is the comparison between the given value K and the root node of the two-point lookup decision tree. If equal, success. Otherwise, Jochiugen the keyword of the node to find in the left subtree. Jordahugen the keyword of the node, find it in the right subtree.
"Example" for a table with 11 nodes, if the found node is the 6th node in the table, then only one comparison is needed, and if the found node is the 3rd or 9th node in the table, two comparisons are required, and the first 1,4,7,10 node needs to be compared three times, and the first 2,5,8,11 node needs to be compared four times.
Thus, the successful binary search process happens to be a path from the root of the decision tree to the node being looked at, and the number of times the keyword has been compared is exactly the level of the node in the tree. If the lookup fails, the comparison process is through a path from the decision tree root to an external node, and the number of keyword comparisons required is the sum of the internal nodes on that path.
The keyword sequence for the "example" unknown origin table is: (05,13,19,21,37,56,64,75,80,88,92), to find the records of k=85, the internal nodes passed were 6, 9, 10, and finally reached the square node "9-10", with a comparison number of 3.
In fact, the meaning of "i-i+1" in square nodes is that the lookup value K is between R[i].key and R[i+1].key, that is, R[i].key<k<r[i+1].key.
average lookup length of ② binary lookup
The total number of internal nodes is n=2h-1, and the decision tree is a full two-fork tree with a depth of H=LG (n+1) (depth h excluding external nodes). The number of nodes on the K-level in the tree is 2k-1, and the number of comparisons required to find them is K. Therefore, in the equal probability hypothesis, the average lookup length when the binary search succeeds is:
ASLBN≈LG (n+1)-1
Binary lookup The number of keywords that are required to compare when a lookup fails does not exceed the depth of the decision tree, and in the worst case, the comparison number of successful lookups does not exceed the depth of the decision tree. That is:
The worst performance and average performance of binary lookups are fairly close.
Advantages of ③ binary search
The time complexity of binary lookup is O (Logn), which is much better than the O (n) of sequential lookups.
Disadvantages of ④ Two-point lookup
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.
⑤ Applicable conditions
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.
Binary search (binary search)