Recently summed up the principle of the major sequencing algorithm, and the implementation of it, thinking of the search algorithm summed up, today began to summarize the search algorithm.
Needless to say, this article starts with the simplest search algorithm, and then complements the complex two-fork search Tree Lookup (BST) and B-tree, the + + tree lookup, and hash lookup.
As the name implies, the search is to find the position of the keyword in the queue, the most stupid search algorithm is sequential comparison, the complexity of O (n), but there are many methods of complexity can reach O (Logn) and so on.
1. Sequential Lookup
The keyword is compared to the number order in the array, and the time complexity O (n).
Template<class t>int ordersearch (t *x, int N, T KeyWord) {for (int i = 0; i < N; i++) {if (x[i] = = KeyWord) return i;} return-1;}
2. Two points Search
Binary search condition is the original array in order, in the search, the key is compared with the number of the middle of the array, if equal to find, if the greater than the search on its right, if less than on its left to look for, and then recursive two-point search.
Time complexity O (logn).
If there are more frequent insert operations, it will take some time to maintain the ordered structure of binary search.
Template<class t>int BinarySearch (t *x, int low, int. high, T keyword)//recursive {if (Low > High) return-1;int mid = (Low + high)/2;if (x[mid] = keyword) return mid;if (X[mid] < keyword) return binarysearch (x, mid+1, high); if (X[mid] > Keywo RD) return BinarySearch (x, Low, mid-1);}
Template<class t>int BinarySearch (t *x, int N, t keyword)//no recursion {int low = 0, high = n-1,mid;while (Low <= high) {mid = (low + high)/2;if (x[mid] = = keyword) return mid;elseif (X[mid] < keyword) Low = mid + 1;elsehigh = mid-1;} return-1;}
3. Block Lookup
Block lookup is an improved method for sequential lookups. First, you need to block the array, block lookup needs to establish an "index table." The index table is divided into m blocks, each containing n/m elements, the blocks are unordered, the blocks are ordered, for example, the largest element in block 2 is less than the smallest element in block 3.
Find the Index table by binary first, determine which keyword you want to find, and then look in the corresponding block in order. block lookups are also known as index order lookups.
Time complexity: O (log (m) +n/m)
Block find Template<class t>//Index table struct indextable{t key;int link;}; Template<class t> indexordersearch (indextable<t> *indextable,t *x, int N, int m, T keyword)// Indextable is the index table, X is the original array, N is the size of the arrays, M is the block size {int L = (n+m-1)/m;int i = 0;while (i < L && indextable[i].key < keyword) i++ if (i = = L) Return-1;else{int j = indextable[i].link;for (j; j<indextable[i].link + m;j++) if (x[j] = = keyword) return j;} return-1;}
Find a simple search of the algorithm family: Sequential lookup, binary lookup, block lookup