A. Find the linear form
1. sequential search
Sequential search starting from the period of the table, followed by the recording of keywords and a given value, if given the same keywords and the value of a record, you find success, on the contrary, if after the scan the entire table, also No keywords and a given value
Equal to the record, the lookup fails.
The book gives two algorithms.
7.1
int search_seq(ssTable ST,Keytype key) { for (i=ST.length;i>=1;i--) if(ST.R[i].key==key) return i; return o; } int search_seq(ssTable ST,Keytype key) { ST.R[0]=key; for (i=ST.length;ST.R[i].key!=key;i--) return i; }
The second algorithm is a first improvement, should be removed on each cycle i> = 0 is determined, thereby reducing the time. However, the time required for the two algorithms are dependent on the size of n, the time complexity is O (n).
Advantages: the algorithm is simple, the structure of the table No.
Disadvantages: the average search length, low efficiency, when the value of n is very large, the low order search efficiency.
2. binary search
Search process from the middle of the array elements, if the intermediate element the element is just looking for, the search process ends; if a particular element is greater than or less than the intermediate element in the array is greater or less than half that of the intermediate element lookup, and Like start start comparing the middle element. If the array is empty at some stage, it represents not found. This search algorithm so that each comparison search reduced by half.
Complexity: T (n-) = O (log 2 (n-)), S (n-) O = (. 1)
When the stored data out of order, first to sort the data using the fast discharge time complexity of O (nlog2 (n)), so the total time complexity is T (n) = O (nlog2 (n)) + O (log 2 (n-)) taking the highest order O = (nlog 2 (n-)).
Advantages: relatively low frequency and high search efficiency.
Disadvantages: can only be used for sequential storage structure, find an object must be ordered array. In the first sort of order data processing, it will be a waste of time. And in order to ensure the order of the data, compare the average insert and delete and move half of the elements, which will be time consuming, difficult to perform dynamic lookup. Second, the amount of data exceeds the memory space can provide continuous, binary search can not be performed.
II. Search tree table
1. binary sort tree
(1) is preferably: O (log 2 n-) - complete binary tree
(2) Worst: O (n) --- Single Tree
III. Hash table
Find previous methods of linear tables and tree table, are based on a comparison of the stored keywords, regardless of the address of the store, a lot of the time when the number of nodes and look to be compared with the large number of invalid nodes, leading to the search speed is very slow.
If we establish a link between keywords and where it is stored, then find when looking for keywords directly in accordance with this relationship, which is thought hash lookup.
(1) and hash function Hash Address: Contact-one correspondence such that p H = H (key) thereof between a storage position p and the key KEY record, H is called a hash function, called a hash address p .