Find Basic concepts:
Lookup, also known as retrieval, refers to the process of finding a record in a batch of records that satisfies a specified condition. In daily life, such as Address Book lookup, dictionary lookup and so on often use the search method , in the program design, the search in many programs takes a lot of time, so a good search method can improve the speed of the program.
primary KEY and secondary keywords:
In the data structure that you need to find, each record typically contains more than one data field. A lookup condition is typically a value of one or more of the fields given, which becomes the keyword (key) for the lookup criteria, or the keyword (Primary key) If the keyword can uniquely identify a record in the data structure; If the keyword cannot uniquely differentiate between different records, This keyword is referred to as the secondary keyword (secondary key). Most commonly used, the main thing is to find the main keyword
Find Results
If a lookup finds a record that corresponds to a given keyword, it indicates that the lookup was successful and that the record was stored in order to process the record (output, modify, delete, etc.). The record corresponding to the given keyword could not be found, indicating that the lookup failed and returned a specific failure value.
static lookup tables and dynamic lookup tables
The data structure used for lookups is called a lookup table and can be divided into static lookup tables and dynamic lookup tables:
Static lookup table: During the lookup process, the data structure does not change the lookup is called the static lookup table, in the static lookup table, after the lookup fails, will return the failure value.
Dynamic lookup table: During the lookup process, the data structure changes with the lookup process is called the Dynamic lookup table, in the dynamic lookup table, after the lookup fails, the lookup table in the original non-existent records are added to the lookup table, in addition, the dynamic lookup table can delete the specified keyword records.
Here are two simple ways to find the structure of the data in a linear structure: sequential lookup and binary lookup (also called binary lookup).
1. Sequential lookup:
Sequential lookup (sequential search) is the simplest way to find a method, the basic idea is: starting at the end of the linear table, the key of each record is compared with the given value, if the keyword equals the given value, the search succeeds, returns the record ordinal, if all the records in the linear table are compared, No records were found that are equal to the given value, the lookup fails, and the failed value is returned.
Cases:
#include <stdio.h>#defineArraylen 8//elements of a static lookup tableintsource[arraylen]={ the, $, -,Panax Notoginseng, the,6, -, Wu};//Static lookup TableintSeqsearch (intS[],intNintkey) { inti; for(i=0; i<n&&s[i]!=key;i++)//Loop Find Keywords ; if(i<n)//find keywords in static lookup table returni; Else return-1;}intMain () {intKey,i,pos; printf ("\ n Please enter a keyword:"); scanf ("%d",&key); POS=Seqsearch (Source,arraylen,key); printf ("original data: \ n"); for(i=0; i<arraylen;i++) printf ("%d", Source[i]); if(pos>=0) printf ("\ n Lookup succeeded, the keyword is in%d locations:", pos+1);//plus 1 Let the position display start from 1 Elseprintf ("\ nthe lookup failed!"); return 0;}
In the circular statement found in the order above, each cycle requires two comparisons (I<n && r[i]!=key), if the data in the static lookup table is many, and every two times the comparison takes a long time, so that the program is less efficient, In this case, the Seqsearch algorithm can be improved, in the creation of a static lookup table, you need to add an empty cell at the end of the table, to save the search keyword, so that the static lookup table has a and find the same data, you can remove the I<n judgment, each time you find, Keywords are always found in the lookup table, but key values are found when the first N+1 element is found, indicating that the lookup failed.
After improvement:
#include <stdio.h>#defineArraylen 8//elements of a static lookup tableintsource[arraylen+1]={ the, $, -,Panax Notoginseng, the,6, -, Wu};//Static lookup TableintSeqsearch (intS[],intNintkey) { inti; for(i=0; s[i]!=key;i++)//Loop Find Keywords ; if(i<n)//find keywords in static lookup table returni; Else return-1;}intMain () {intKey,i,pos; printf ("\ n Please enter a keyword:"); scanf ("%d",&key); Source[arraylen]=key;//save key to lookup table lastpos=Seqsearch (Source,arraylen,key); printf ("original data: \ n"); for(i=0; i<arraylen;i++) printf ("%d", Source[i]); if(pos>=0) printf ("\ nthe lookup succeeded, the keyword is in the location of%d:", pos+1);//plus 1 Let the position display start from 1 Elseprintf ("lookup failed \ n"); return 0;}
The above code line 22nd will be saved to the end of the lookup table, the key value of one one and the lookup table in comparison with the elements, when the comparison succeeds before the 9th element, the search succeeds, when the location of the 9th position that key is saved, can certainly match the success, but this time is 9th, indicates that the lookup failed. This procedure eliminates the i<n comparison.
Sequential Lookup Benefits: There is no requirement for the order of data in a static lookup table, and when you need to create a dynamic lookup table, you can easily add lookup-unsuccessful data to the end of the lookup table.
The disadvantage of sequential lookup tables: Slow, in the worst case, find success requires n This comparison, find failure to n+1 this comparison.
2. Binary search:
Binary lookup is also called Binary lookup, which requires that the data in the lookup table be stored in a linear structure, and that the data in the lookup table is ordered alphabetically by keyword (from small to large or from large to small)
The following is an orderly arrangement of the data from small to large n elements to introduce the binary lookup process:
The first is the ordinal m (M=N/2) of the position element located in the middle of the lookup table, and the keyword for s[m] compared to the given value key, there are three possible:
- If S[m]=key, indicates a successful search.
- If S[m]>key, indicates that the keyword key is between the minimum and middle values, and the first half of the binary is searched.
- If S[m]<key, indicates that the keyword key is between the maximum value and the middle value, and the second half of the binary is searched.
As seen from the above process, binary lookup is a recursive process, each time binary, can make the scope of the search to reduce by half, when the scope of the search narrowed to only one element, and the element is not equal to the keyword, indicating that the lookup failed.
in the worst case, the binary lookup requires an O (Logn), and the lookup efficiency is much faster than the sequential lookup.
For example, there are several data: 6,12,28,37,54,65,69,83,90,92, with the PNS as the keyword, to find:
Process:
The following program is written to implement the binary lookup process:
#include <stdio.h>#defineArraylen 10intsource[]={6, A, -,Panax Notoginseng, Wu, $, the, the, -, the};intBinarySearch (intS[],intNintkey) { intLow,height,mid; Low=0; Height=n-1; while(Low<=height)//The lookup scope contains at least one element{mid= (low+height)/2; if(s[mid]==key)returnMid//return ordinal Else if(s[mid]>key) Height=mid-1; Else Low=mid+1;//Define a lookup range } return-1;//Return lookup failed}intMain () {intKey,i,pos; printf ("\ n Please enter the keyword you are looking for: \ n"); scanf ("%d",&key); POS=BinarySearch (Source,arraylen,key); printf ("Original data:"); for(i=0; i<arraylen;i++) printf ("%d", Source[i]); printf ("\ n"); if(pos>=0) printf ("\ nthe lookup succeeded, and the keyword is located in the first%d location. \ n", pos+1); Elseprintf ("lookup failed \ n"); return 0;}
The advantage of binary lookup is that the average performance is good, the search speed is fast, the maximum number of lookups is O (log (n)), you can quickly approximate the keyword.
The disadvantage of binary lookup is that there are requirements for data arrangement in the lookup table, first in order, and ordered (from small to large or from large to small), so the lookup table needs to be sorted before using binary , and when the Find keyword is unsuccessful, If you need to add this keyword to a lookup table, you need to move large amounts of existing data, as well as delete large amounts of data.
Lookup algorithm--a method for finding linear structures