From: http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.2.2.1.htm
Binary Search
1. Binary Search)
Binary Search, also known as semi-query, is an efficient search method.
Binary Search requirements: A linear table is an ordered table, that is, the nodes in the table are ordered by keywords, and vectors are used as the storage structure of the table. You can set an ordered table to be incremental and ordered.
2. Basic Idea of Binary Search
The basic idea of binary search is: (Set R [low .. High] to the current search range)
(1) first, determine the midpoint of the interval:
(2) then compare the value of K to be queried with R [Mid]. key comparison: if they are equal, the search is successful and the location is returned. Otherwise, you must determine a new search interval and continue the binary search. The specific method is as follows:
① If R [Mid]. key> K, the order of the table indicates R [Mid .. n]. keys are greater than K. Therefore, if a node with the keyword K exists in the table, the node must be in the child table R [1 .. mid-1], so the new search interval is the left sub-Table R [1 .. mid-1].
② Similarly, if R [Mid]. key <K, the K to be searched must be in the right sub-Table R [Mid + 1 .. n], that is, the new search interval is the right sub-Table R [Mid + 1 .. n]. The next search is for the new search interval.
Therefore, from the initial search interval r [1 .. n] At the beginning, you can determine whether the search is successful after a comparison with the node keyword at the midpoint of the current search interval. If the search interval is unsuccessful, the current search interval is halved. This process repeats until the node with the keyword K is found, or until the current search interval is null (that is, the search fails.
3. Binary Search Algorithm
Int binsearch (seqlist R, keytype K)
{// Perform a binary search in the ordered table R [1. N]. If the query succeeds, the node location is returned. If the query fails, zero is returned.
Int low = 1, high = N, mid; // set the initial values of the upper and lower bounds of the current search range.
While (low <= high) {// the current search range R [low .. High] is not empty
Mid = (low + high)/2;
If (R [Mid]. Key = k) return mid; // return if the search is successful
If (R [Mid]. kdy> K)
High = mid-1; // continue searching in R [low... mid-1]
Else
Low = Mid + 1; // continue searching in R [Mid + 1 .. High]
}
Return 0; // when low> high, it indicates that the search interval is null and the search fails.
} // Binseareh
The binary search algorithm is also easy to provide its recursive program. [See exercise]
4. Execution Process of the Binary Search Algorithm
Set the order keyword sequence in the input instance of the algorithm
)
The key words K to be searched are 21 and 85 respectively. Specific search process [see animation demonstration]
5. Binary Search Decision Tree
The binary search process can be described using a binary tree: the node in the middle of the current search interval is used as the root, and the node in the left sub-table and right sub-table are used as the left sub-tree and right sub-tree of the root respectively. The resulting binary tree is called a decision tree or a comparison tree that describes binary search ).
Note:
The decision tree is only related to the number of table nodes N, but not to the value of R [1. N]. Keys in the input instance.
[Example] An ordered table with 11 nodes can be represented by the decision tree shown in.
(1) Composition of the Binary Search Decision Tree
① A circular node is an internal node in the tree. The number in the circle node in the tree indicates the position of the node in the ordered table.
② External node: All null pointers in a circular node are replaced by a virtual square node, that is, an external node.
③ Mark "<", "("> ",") "on the left (right) branch of a node I connected to the left (right) child in the tree: when the keyword k <R [I] is to be queried, key (k> r [I]. key), you should take the left (right) Branch to reach the left (right) child of I, and further compare the child's keywords with K. If they are equal, the search process ends and returns. Otherwise, K is compared with the node at the next layer in the tree.
(2) binary search decision tree search
Binary Search compares the keyword of the given value K with the root node of the Binary Search decision tree. If they are equal, the operation is successful. Otherwise, if it is less than the keyword of the root node, search in the left subtree. If it is greater than the keyword of the root node, search in the right subtree.
[Example] for a table with 11 nodes, if the queried node is 6th nodes in the table, only one comparison is required. If the searched node is 3rd or 9th nodes in the table, perform secondary comparison. Search for nodes, and 10 must be compared three times. Find nodes, must be compared four times.
It can be seen that the successful binary search process is just a path from the root of the decision tree to the queried node. The number of keywords that have been compared is exactly the number of layers of the node in the tree. If the search fails, the comparison process goes through a path from the decision tree root to an external node. The number of key words required for comparison is the total number of internal nodes on the path.
[Example] The sequence of keywords in the table to be queried is: (, 13,). to query records with K = 85, the internal nodes that pass through are 6, 9, and 10, and finally reach the square node "9-10". The number of comparisons is 3.
In fact, the meaning of "I-I + 1" in a square node is that the queried value K is between R [I]. key and R [I + 1]. key, that is, R [I]. key <k <R [I + 1]. key.
(3) average length of Binary Search
If the total number of internal nodes is n = 2h-1, the decision tree is a full binary tree with a depth of H = lg (n + 1) (depth H is not counted as an external node ). The number of nodes on the K layer in the tree is 2k-1, and the number of comparisons required for finding them is K. Therefore, under the equi probability hypothesis, the average length of a binary search is:
Aslbn ≈ lg (n + 1)-1
The number of keywords to be compared when the search fails cannot exceed the depth of the decision tree. In the worst case, the number of successful comparisons cannot exceed the depth of the decision tree. That is:
The worst performance of binary search is quite similar to the average performance.
6. Advantages and Disadvantages of Binary Search
Although binary search is highly efficient, you need to sort the table by keywords. Sorting itself is a very time-consuming operation. It takes O (nlgn) Time to adopt an efficient sorting method.
Binary Search only applies to sequential storage structures. To keep the table in order, a large number of nodes must be moved in the sequence structure. Therefore, binary search is especially suitable for linear tables that are rarely changed once created and frequently needed to be searched.
For linear tables with few and frequent changes, you can use the linked list as the storage structure for sequential search. Binary Search cannot be implemented on the linked list.
Go to: search for a linear table-Binary Search