Sequential lookup (Sequential search)

Source: Internet
Author: User

1. Definition

Sequential lookups, also called linear lookups , are the most basic search techniques.

2. Basic Ideas

Starting at one end of the table (first or last record), sequentially scanning the linear table, sequentially comparing the scanned node key to the given value K. If the currently scanned node keyword is equal to K, the lookup succeeds; If a node with a keyword equal to K is not found after the scan is completed, the lookup fails.

3. Storage structure

The sequential lookup method applies both to the sequential storage structure of linear tables and to the chain storage structure of linear tables (when using a single-linked list as the storage structure, the scan must begin with the first node).

Note: Why a single-linked list is from the first scan, not the last, which is related to its storage structure, the single-linked list name represents the address of the first first node, not the address of the last node.

4. Sequential Lookup Algorithm(1) type description
  typedef struct{    KeyType key;    infotype otherinfo;//This type depends on the application   }nodetype;  typedef NodeType SEQLIST[N+1] ;//unit No. 0 used as Sentinel
(2) Specific algorithms
/* Sequential lookup, parameter description:    a--array,    n--the number of arrays to find,    key--the keyword to find */int seqsearch (int *a,int n,int key)//Here is the pointer reference {     int i;     for (i=1;i<=n;i++) {    //defect: Each cycle requires that I be out of bounds, i.e. whether it is less than or equal to N to determine        if (a[i]=key)            return i;    }    return 0;     }

In the above operation, each cycle needs to whether I cross-boundary, that is, whether or not equal to N to determine, we can set a sentinel, do not need each time I and N to compare, the improvement scheme is as follows:

/* have Sentinel Order lookup */int seqsearch (int *a,int n,int key)//Here is pointer reference {     int i;     a[0]=key;    /* Set a[0] As the keyword value, which we call "Sentinel", of course, you can also set the last element as "Sentinel" */    int n;    /* loop starts at the end of the array *    /while (A[i]!=key)    {        i--;    }    return i;/     * Returns 0 Description lookup failed */    }

Of course parameters can also be set as follows, the number of elements in the data structure is defined in the body:

int Seqsearch (seqlist r,keytype K) {    //In order table R[1..N] In order to find the node with the keyword K,   //To return the found node position on success, return 0 int i on failure, and   r[0]. key=k;    //Set Sentinel   for (i=n;r[i].key!=k;i--);//To    look forward from the table   return i;    //If I is 0, which means the lookup fails, otherwise r[i] is the node to find}
3. Algorithm Analysis

the role of monitoring whistle r[0] in the ① algorithm

In order to eliminate the criteria i≥1 to prevent the subscript from crossing out inthe For loop, the comparison time is saved.

② the average lookup length for sequential lookups when successful:

In the case of equal probability, pi=1/n (1≤i≤n), the average search length of success is

(n+...+2+1)/n= (n+1)/2

That is, the average number of comparisons when finding success is about half the length of the table.

If the K value is not in the table, a n+1 comparison is required before the lookup failure can be determined.

③ the probability of finding each node in a table is not equal to the ASL

"Example" in a linear table composed of medical records of the whole school students, the ill-health of the patient's medical records to find the probability is higher than the healthy classmate's record, because the ASLSQ in the pn≥pn-1≥ ... The minimum value is reached when ≥p2≥p1.

If you know in advance that the lookup probabilities of each node in the table are unequal and their distribution, you should store the nodes in the table as small as the lookup probability, so as to improve the efficiency of sequential lookups.

To improve the efficiency of the search, the algorithm Seqsearch is modified as follows: Whenever the search succeeds, the nodes that are found and their successors (if any) are exchanged. In this way, the finding of a node with large probability is moved backward in the search process, which makes it easier to reduce the number of comparisons in future searches.

Advantages of ④ sequential lookups

The algorithm is simple, and there is no requirement for the structure of the table, whether it is using vectors or linked lists to store nodes, or whether the nodes are ordered by the keyword, it is equally applicable.

Disadvantages of ⑤ sequential lookups

search efficiency is low , so when n is larger it is not advisable to use sequential lookups.

Applicable conditions

For those linear tables that find little and often need to be changed, a chain list can be used as a storage structure for sequential lookups.

Sequential lookup (Sequential search)

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.