1. Sequential Search
Time complexity: O (N)
Pros: Simple algorithm, no requirement for lookup table records
Cons: Low efficiency
Apply: Find when data volume is low
Principle:
Finds the exact location of the same number as a given keyword in a known no (or ordered) Order queue. The principle is to have the keyword compare with the number in the queue from the last one until you find the same number as the given keyword.
int sequencesearch (int *array, int size, int key) { int I; for (I=0 ; i<size; i++ if (Array[i]==key) { return I; }} return -1
int Sequencesearch (intintint key) { int i=size-1; array[0]=key; Sentinel while (key = Array[i]) { i--; } return i;}
2, two-point search/binary find
Complexity of Time: O (LOGN)
Advantages: Less comparison, faster search speed, good average performance;
Disadvantage: Requires the unknown origin table to be an ordered table, and insert delete difficult.
Applicable: The binary lookup method is suitable for an ordered list that is frequently searched for infrequent changes.
Principle:
First, suppose that the elements in the table are arranged in ascending order, comparing the keywords in the middle position of the table with the lookup keywords, and if they are equal, the lookup succeeds; otherwise, the table is divided into the front and the last two sub-tables with the intermediate positional records, and if the middle position record keyword is greater than the Find keyword, the previous child Otherwise, find the latter child table further. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.
intBinarySearch (int*array,intSizeintkey) { intFirst,last,middle; First=0; Last=size; while(first<=Last ) {Middle= (first+last)/2; if(Array[middle] <key) first=middle+1; Else if(Array[middle] >key) last=middle-1; Else returnMiddle; } return-1;}
3. Interpolation Search
Complexity of Time: O (LOGN)
Applicable: The data volume is large, and the keyword distribution and more uniform lookup table
Principle:
Looking for the word "worst" in the English-Chinese dictionary, we will never look for the element in the middle of the dictionary, and then find the element in the dictionary three-fourths and so on. In fact, we started looking around the desired address (in the very back of the dictionary).
intInsertsearch (int*array,intSizeintkey) { intfirst,last,position; First=0; Last=size-1; while(first<=Last ) {Position= first+ (Last-first) * (Key-array[first])/(array[last]-Array[first]); if(Array[position] <key) first=position+1; Else if(Array[position] >key) last=position-1; Else returnposition; } return-1;}
4. Fibonacci Search
Complexity of Time: O (LOGN)
Advantages: The average performance is better than binary search;
Disadvantage: The lookup table required to be found must be stored sequentially and ordered, and the condition should be fulfilled: if the number of elements in an ordered table is n, and N is exactly (a Fibonacci number-1), the Fibonacci lookup method can be used.
Principle:
#include <stdio.h>#include<stdlib.h>#include<string.h>intf[ -];intFibonaccisearch (int*a,intNintkey) { intlow,high,mid,i,k=0; Low=1;/*define lowest subscript to record first*/ High=n;/*define highest subscript for record*/ while(n>f[k]-1) K++; for(i=n;i<f[k]-1; i++) A[i]=A[n]; while(low<=High ) {Mid=low+f[k-1]-1; if(key<A[mid]) { High=mid-1; K=k-1; } Else if(key>A[mid]) { Low=mid+1; K=k-2; } Else { if(mid<=N)returnMid/*if equal, mid is the location to find it*/ Else returnN; } } return-1;}intMain () {inti; intarr[]={0,1, -, -, *, -, -, +, the, the, About}; f[0]=0; f[1]=1; for(i =2; I < -; i++) {F[i]= f[i-1] + f[i-2]; } printf ("%d\n", Fibonaccisearch (arr,sizeof(arr)/sizeof(int)-1, About)); return 0;}
5. Hash Lookup
Time complexity: O (1)
Applicable: The data itself is not sorted, cannot be compared
Principle:
A method of directly locating a data element by recording the relationship between the data element and the storage address.
#include"stdio.h"#include"stdlib.h"#include"io.h"#include"math.h"#include"time.h"#defineOK 1#defineERROR 0#defineTRUE 1#defineFALSE 0#defineMAXSIZE 100/* Storage space Initial allocation */#defineSUCCESS 1#defineUnsuccess 0#defineHashsize 12/* Defines the length of the hash list as an array */#defineNULLKEY-32768typedefintStatus;/*Status is the type of the function, whose value is the function result status code, such as OK, etc.*/typedefstruct{ int*elem;/*Data Elements Store base addresses, dynamically allocated arrays*/ intCount/*current number of data elements*/}hashtable;intm=0;/*hash table length, global variables*//*Initialize Hash table*/Status inithashtable (HashTable*H) { inti; M=hashsize; H->count=m; H->elem= (int*)malloc(m*sizeof(int)); for(i=0; i<m;i++) H->elem[i]=Nullkey; returnOK;}/*hash Function*/intHash (intkey) { returnKey% M;/*In addition to the residue remainder method*/}/*insert keyword into hash list*/voidInserthash (HashTable *h,intkey) { intaddr = Hash (key);/*Request Hash Address*/ while(H->ELEM[ADDR]! = Nullkey)/*if it is not empty, the conflict*/{addr= (addr+1)% m;/*linear detection of open addressing method*/} H->ELEM[ADDR] = key;/*Insert the keyword until you have a vacancy*/}/*Hash list Find keywords*/Status Searchhash (HashTable H,intKeyint*addr) { *ADDR = Hash (key);/*Request Hash Address*/ while(H.ELEM[*ADDR]! = key)/*if it is not empty, the conflict*/ { *addr = (*addr+1)% m;/*linear detection of open addressing method*/ if(H.elem[*addr] = = Nullkey | | *addr = = Hash (key))/*If the loop goes back to the original point*/ returnunsuccess;/*indicates that the keyword does not exist*/ } returnSUCCESS;}intMain () {intarr[hashsize]={ A, the, About, -, -,Panax Notoginseng, A, in, the, -, -, the}; intI,p,key,result; HashTable H; Key= the; InitHashTable (&i); for(i=0; i<m;i++) Inserthash (&H,arr[i]); Result=searchhash (h,key,&p); if(Result) printf ("the address for the lookup%d is:%d \ n", key,p); Elseprintf ("failed to find%d. \ n", key); for(i=0; i<m;i++) {Key=Arr[i]; Searchhash (H,key,&p); printf ("the address for the lookup%d is:%d \ n", key,p); } return 0;}
Summary of common search algorithms