From the beginning of this article to introduce the algorithm in the search technology. Find in our lives everywhere, such as check bus, check air tickets, check Hotel ... These are all lookups.
First look at the classification of search techniques. Such as:
1. Sequential Lookup
So this article summarizes the sequential lookup techniques in the sequential table.
What is sequential lookup? The principle of sequential lookup is simple, is to traverse the entire list, the key is recorded by the comparison with the given value, if a record of the keyword and the given value is equal, the search succeeds, find the record. If the key and the given value are not equal until the last record, the table does not have the records found and the lookup fails.
2. Two points Search
Binary lookup also belongs to the sequential table lookup range, and the binary lookup is also known as binary lookup. The time complexity of binary lookup (ordered) is O (Logn).
So what is binary search? The basic idea of binary search is that, in an ordered table, the intermediate record is taken as the comparison object, if the given value is equal to the key word in the middle record, the search succeeds if the given value is less than the middle record, the left half of the intermediate record is searched, and if the given value is greater than the middle record, the right half of the Repeat the process until you find it.
From the definition of binary lookup we can see that there are two prerequisites for using a binary search:
1, the list you want to find must be ordered.
2, the data must be stored using the sequential storage structure of the linear table.
3. Index Lookup
In this article we want to summarize the index lookup, about the index, we easily associate with the index in the database, set up an index, can greatly improve the database query speed.
Index lookup, also known as Block lookup, is a search method between sequential lookups and binary lookups, and the basic idea of block lookup is to find the index table first, use binary lookup or sequential lookup, and then search sequentially in the determined block.
The time complexity of block lookups is O (√n).
The following three terms need to be clarified before implementing an index lookup algorithm.
1, Main table. The object that you want to find.
2, index entry. In general, we divide the main table into several sub-tables, each of which creates an index, which is called an index entry.
3, Index table. That is, the collection of indexed items.
At the same time, the index entry includes the following three points.
1,index, which is the keyword of the index pointing to the main table.
2,start, which is the position of index in the primary table.
3,length, which is the interval length of the child table.
4. Two fork sorting tree
This article begins by summarizing the two-fork sort tree. The purpose of constructing a binary sort tree is not to be ordered, but to improve the efficiency of finding and inserting deletes.
So what is a two-fork sorting tree? Binary sorting tree has the following characteristics.
1, if the root node has a left dial hand tree, all nodes of the left subtree are smaller than the root node.
2, if the root node has a right subtree, all nodes of the right subtree are larger than the root node.
3, the left and right subtree of the root node are also two-fork sort trees respectively.
Here is an illustration of a two-fork sort tree, which allows you to deepen your understanding of the two-fork sort tree.
Here are the common operations and ideas of the two-fork sorting tree.
1, inserting nodes
Idea: For example, we want to insert the number 20 into this binary sort tree. Then the steps are as follows:
1) First compares the 20 with the root node and discovers that it is smaller than the root node, so it continues to be compared to the left subtree 30 of the root node.
2) found 20:30 is also small, so continue with 30 of the left subtree 10 to compare.
3) 20:10 is found to be large, so 20 is inserted into the right subtree of 10.
Binary sort tree effect at this time
2, finding nodes
For example, we want to find node 10, then the idea is as follows:
1) Still the same, first compares 10 with the root node 50, and discovers that it is smaller than the root node, so it continues to be compared to the left subtree 30 of the root node.
2) found that 10 is smaller than the left subtree 30, so continue to compare with 30 of the left subtree 10.
3) Two values are found to be equal, that is, the lookup succeeds and the position of 10 is returned.
The procedure is the same as the insert, it is not mapped here.
3, deleting nodes
The deletion of nodes is relatively complex, mainly in the following three scenarios:
1) Delete the leaf node (i.e. No child node). For example, 20, delete it will not destroy the original structure of the tree, the simplest.
2) Delete the single child node. For example, 90, deleting it will need to connect its child node to its parent node. The situation is more complicated than the first.
3) Delete the nodes that have left and right children. For example, root node 50, here is a problem is to delete it after the problem of who do the root node? The middle sequence traversal of the binary tree is the leftmost child of the left subtree of the right node.
After the analysis, with the idea, the following began to write code to implement these functions.
5. Hash Lookup
This article summarizes the last of the five lookups, hash lookup, also known as Hash lookup (this article is called hashed). To raise the hash, my first impression is the Hashtable class in C #, which is a set of Key/value key-value pairs, which is the hashing technique applied.
So, what is hash lookup? Before figuring out what a hash lookup is, we need to figure out Chuhashi technology, where the hashing technique establishes a definite correspondence between the record's storage location and the recorded keyword, so that each keyword key corresponds to a storage location F (key). When looking, the mapping F (key) of the given value is found based on this determined correspondence, and must be at f (key) if the record exists in the lookup collection. Hashing technology is both a storage method and a lookup method.
Six ways to construct hash functions:
1, direct addressing method:
function formula: F (key) =a*key+b (A, B is constant)
The advantage of this approach is that it is simple, uniform, and does not create conflicts. However, it is necessary to know the distribution of the keywords in advance, which is suitable for finding small and continuous tables.
2, Digital analysis method:
For example, our 11-digit mobile phone number "136xxxx7887", of which the first three is the access number, generally corresponds to the sub-brand of different operating companies, such as 130 is Unicom Ruyi, 136 is mobile Shenzhou line, 153 is telecommunications. The middle four are the HLR identification number, which indicates the user's attribution. The last four are the real user numbers.
If we want to store a company employee registration form Now, if we use the mobile phone number as the keyword, then most likely the first 7 bits are the same, so we choose the following four as the hash address is a good choice.
3, square take the middle method:
So the name of meaning, such as the keyword is 1234, then its square is 1522756, and then extract the middle of the 3 is 227 as the hash address.
4, Folding Method:
The folding method is to divide the keyword from left to right into several parts equal to the number of digits (the last part is not short enough), and then add the portions to the hash table, and take the following several as hash addresses.
For example, our keyword is 9876543210, the hash table is three bits long, we divide it into four groups, 987|654|321|0, and then add them to sum 987+654+321+0=1962, and then ask for 3 bits to get the hash address of 962, haha, is not very interesting.
5, in addition to the remainder method:
function formula: F (key) =key mod p (p<=m) m is the hash table length.
This method is the most common method of constructing a hash function.
6, Random number method:
function formula: F (key) = random (key).
Here the random function, when the length of the keyword is not equal, the use of this method is more appropriate.
Two methods of resolving hash function conflicts:
Our best-designed hashing function is also unlikely to completely avoid collisions, and when we use a hash function we find two keyword Key1!=key2, but there is an F (key1) =f (Key2), which is a conflict.
Method One: Open addressing method:
The open addressing method is to look for the next empty hash address once a conflict occurs, as long as the hash table is large enough, the empty hash address is always found, and the record is inserted. This approach is the most common approach to conflict resolution.
Method Two: Chain address method:
Here is the implementation code:
The basic Knowledge Series 5--five big find