Algorithm Learning Search (order, dichotomy, sort binary tree, and Hash table)

Source: Internet
Author: User

Summary Lookup-Identifies a data element with a keyword and finds a record or data element in the table that has a key value equal to the given value, based on a given value. The method of finding in a computer is determined by the organizational structure of the records in the table. Find a basic function of function data processing. Data lookup is not complicated, but how can you find it quickly and easily? Some of the methods accumulated by the predecessors in practice are worth studying. We assume that the data being found is unique and there are no duplicate data in the array.

1. Sequential Search

Imagine having a 1M data, how we can find the data we want in it. The data itself has no characteristics at this point, so the data we need may appear in various locations in the array, possibly at the beginning of the data, and possibly at the end of the data. This nature requires that we have to traverse the data before we can get the corresponding data.

  intFindintArray[],int  length,intValue) {if(NULL = = Array | |0==length)return-1; for(int Index=0;Index<length;Index++){if(Value = = array[Index])return Index; }return-1;}

Analysis:
As we are not sure how many times this data will be judged. However, we know that such a data lookup requires at least 1 times, then the maximum number of n times, the average can be seen as (1+n)/2, almost half of N. We write an O (n) to the algorithm complexity that is proportional to the comparison number and N.

The data above does not have any characteristics, which leads to a disorderly arrangement of our data. Imagine what it would be like if the data were arranged in a very neat way. Just like in life, if you usually do not pay attention to tidy up, then find something very troublesome, inefficient; but once the place is fixed, everything is sorted out, then the result is different, we will form a mindset, so the efficiency of finding things will be very high. So, what should we look for in an ordered array? The dichotomy is the best method.

2, two-point search
intBinary_sort (int Array[],intLengthint value){if(NULL = =Array||0= = length) return-1;intStart =0;int End= Length-1; while(Start <=End){intMiddle = start + ((End-start) >>1);if(value==Array[Middle]) return middle;Else if(value>Array[Middle]) {start = middle +1; }Else{End= Middle-1; }} Return-1;}

Analysis:
As we said above, the complexity of the common data lookup algorithm is O (n). The binary lookup algorithm has an O (log (n)) complexity.

This method is at least 1 times, so how many times does it take? We found that a maximum of log2n+1 needed to be log2n down, plus 1. We can find an example to figure out, for example, 7 data, we found a maximum of 3 times, if it is 15 data, then up to 4 times, it is obvious that this data lookup is much more efficient than the previous search method.

The above lookup is built on the basis of continuous memory, so what if it is a pointer type of data? What do we do? Then we need to introduce a sort of binary tree.
(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than the value of its root node, (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node, and (3) the left and right subtrees are also two fork sort trees.

3. Sort two-fork tree
typedefstruct _NODE{    int data;    struct _NODE* left;    struct _NODE* right;}NODE;
//Const is what the compiler checks for properties, only the number is found, and the element value cannot be modifiedConst NODE*Find_data (const NODE*Pnode, intData){if(NULL ==Pnode)return NULL;if(Data ==Pnode -Data)returnPnode;Else if(Data <Pnode -Data)returnFind_data (Pnode -LeftData);Else        returnFind_data (Pnode -RightData); }

Analysis:
Similarly, we see that binary and binary trees are built on the basis of a complete sequence, so is there any search based on compromise? Yes, that's the hash table.
The hash table is defined as follows:
1) Each data is grouped into a large class according to some clustering operation, then all the data is linked to a list;
2) The head pointers of all the linked lists form an array of pointers. This approach is effective when working with medium-sized data because it does not require a full ordering. The nodes are defined as follows:

4. Hash table
typedefstruct _LINK_NODE{    int data;    struct _LINK_NODE* next;}LINK_NODE;

So how do you find the data below the hash table?

link_node* hash_find (link_node* array[], int  mod , int  data) {    int  index  = data% mod ; if         (NULL = = Array[index ]) return     NULL;    link_node* Plinknode = Array[index ]; while  (Plinknode) {if  (data = = Plinknode->data) return Plinknode;    Plinknode = plinknode->next; } return  plinknode;}  

Analysis:
Algorithm complexity O (1). Hash table because do not need to sort, only a simple collation, in the data search is particularly convenient. The size of the find time depends on the size of the MoD. The smaller the MoD, the closer the hash lookup is to the normal lookup; then the larger the hash, the greater the probability of a successful hash search.

5. Summary

1, sequential lookup: sequentially from the beginning of the sequence from start to end, this is the sequential lookup, is the simplest way to find.

2, binary find (binary search): The precondition: The sequential storage structure, must be the size of the key word order.

3, Binary search tree: It is an empty tree or a tree with the following properties: its left and right subtree is also a binary search tree, and the value of the left subtree is less than the root node, the value of the tree is greater than the root node. Therefore, according to such a structure, first based on the comparison with the root node, in determining the left subtree or the right subtree to find, narrow the search scope.

4, Hash: In the process of hash lookup, only the data to be looked for is mapped to its hash value, and then find the data with this hash value, which greatly reduces the number of lookups.

Algorithm Learning Search (order, dichotomy, sort binary tree, and Hash table)

Related Article

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.