Data structure 49: Sequential lookup algorithm

Source: Internet
Author: User

The static lookup table is a lookup table that does only the lookup operation, as described by the front-facing static lookup table.

A static lookup table can be represented either by using a sequential table or by using a linked list structure. Although the one is an array, a list, but the two in the search operation, basically the same.

This section provides a detailed introduction to the sequential storage structure of a static lookup table.

An implementation of sequential lookups when a static lookup table is represented by a sequential storage structure, the lookup process for sequential lookups is: Starting with the last data element in the table, comparing the key words with the record, and if the match is successful, the lookup fails if the first keyword in the table has not been successfully matched.

The specific implementation code for the sequential lookup is:
#include <stdio.h>#include<stdlib.h>#defineKeyType int
typedefstruct
{KeyType key; //find the value of each data element in a table//You can also add additional properties if you want}elemtype;typedefstruct
{elemtype*elem;// An array that holds the data elements in the lookup table intLength// record the total number of data in a lookup table}sstable;// Create a lookup tablevoidCreate (Sstable **st,intlength)
{ (*ST) = (sstable*)malloc(sizeof(sstable)); (*st)->length =length; printf ("data elements in the input table: \ n"); // based on the total length of the data element in the lookup table, stores the data from a space labeled 1 from the array when it is stored for(intI=1; i<=length; i++)
{scanf ("%d", & (*st)Elem[i].key)); }}
// Find a Function function for table lookup, where key is the keywordintSEARCH_SEQ (sstable *St, KeyType Key)
{St->elem[0].key = key;// Store the keyword as a data element in the first position of the lookup table, the role of the watch whistle inti = st->length; // iterate through the last data element of the lookup table, traversing to the array subscript 0 while(St->elem[i].key! =key)
{i--; } //if i=0, the lookup fails, and the return is the position of the data element containing the keyword key in the lookup table returni;}
intMainintargcConst Char*argv[])
{sstable*St; Create (&st,6); GetChar (); printf ("Please enter a keyword to find the data: \ n"); intkey; scanf ("%d", &key); intlocation=Search_seq (St, key); if(Location = =0)
{printf ("Find failed"); }
Else
{printf ("The location of the data in the lookup table is:%d", location); }
return 0;}
The running code sets a fixed length of6The order table, for example, in the lookup table for {1,2,3,4,5,6Find the keyword1the location of the data element, the result is: The data element in the input table:1 2 3 4 5 6Please enter a keyword to find the data:2The location of the data in the lookup table is:2

At the same time, when the lookup table is initialized in the program, all data elements are stored in the array because of sequential storage, but the first position is left to the user for the keyword to find. For example, in the sequential table {1,2,3,4,5,6}To find elements with a data element value of 7, the order table is added:
Figure 1 The lookout in the sequential table
One end of the sequential table adds a keyword that the user uses to search, called the "lookout." The position of the Sentinel in Figure 1 can also be placed after the data element 6 (in which case the entire lookup order should be reverse lookup to sequential lookup). After placing the watch, the sequential table traversal takes place from one end without the monitor, and if the lookup table has the data that the user needs, the program outputs the location, whereas the program runs to the watchdog, where the match succeeds and the program stops running, but the result is a lookup failure. Performance analysis of sequential lookups the performance analysis of a lookup operation focuses on its time complexity, while the entire lookup process spends most of its time comparing the data in the keyword and lookup tables.

So the search algorithm is good or bad based on: When the search succeeds, the searched keyword and the data element in the lookup table are averaged over the number of comparisons, called average lookup lengths (Average search length, represented by ASL).

For example, for a lookup table with n data elements, the average lookup length for the lookup success is calculated as:

The probability of the Pi being looked up for the first data element, the probability of all elements being looked up, and the number of 1;ci that have been compared before finding the first Data element. If there are n data elements in the table, you need to compare n times to find the first element, and you need to compare 1 times to find the last element, so there are Ci = n – i + 1。 In general, the probability of each data element in the table being looked up is unknown. Assuming that the lookup table contains n data elements, the probability of each data being looked up is the same:

After conversion, you have to:

If the probability of finding each data element in a lookup table is known in advance, the data elements in the lookup table should be appropriately adjusted according to the size of their lookup probabilities: the greater the probability of being looked at, the nearer it is to find the starting point I, the farther away. This can reduce the number of comparisons in the lookup operation appropriately.

The average lookup length above is based on the assumption that the search algorithm succeeds every time. For the lookup algorithm, the probability of finding success and finding failure is the same. Therefore, the average lookup length of the lookup algorithm should be the average lookup length when the lookup succeeds plus the average lookup length when the lookup fails.

For a table with n data, each lookup fails, and the number of comparisons is n+1. So the average lookup length of the search algorithm is calculated as:
This section mainly introduces the implementation of the sequential storage representation and lookup algorithm of static lookup table, which is used to improve the traversal algorithm of ordinary sequential table, and can effectively improve the efficiency of the algorithm in the case of large data volume.

Data structure 49: Sequential lookup algorithm

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.