Turn: Linear table search-sequential search

Source: Internet
Author: User

From: http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.2.1.htm

Sequential search)

In the table organization mode, linear tables are the simplest. Sequential search is the simplest Search Method.

1. Basic Idea of sequential search
The basic idea is: from the end of the table, scan the linear table sequentially and compare the key Yu of the scanned node with the given value K in sequence. If the keywords of the node currently scanned are the same as those of K, the search is successful. If no node with the keyword K is found after the scan, the search fails.

2. Storage Structure requirements for sequential search
The sequential search method is applicable to both the sequential storage structure of a linear table and the chain storage structure of a linear table. When using a single-chain table as a storage structure, scanning must start from the first node ).

3. Sequential Search Algorithm Based on Ordered Structure
(1) type description
Typedef struct {
Keytype key;
Infotype otherinfo; // This type depends on the Application
} Nodetype;
Typedef nodetype seqlist [n + 1]; // unit 0 is used as a sentry

(2) specific algorithms
Int seqsearch (seqlist R, keytype K)
{// In the sequence table R [1. N], locate the node with the keyword K,
// If the node is successful, the node location is returned. If the node fails, 0 is returned.
Int I;
R [0]. Key = K; // set the sentry
For (I = N; R [I]. Key! = K; I --); // locate from the back of the table
Return I; // If I is 0, the query fails. Otherwise, R [I] is the node to be searched.
} // Seqsearch
High-end ordered lookup of monitoring record (see exercises]

(3) algorithm analysis
① Role of monitoring record R [0] In the algorithm
In order to save the condition I ≥1 IN THE for loop, which prevents the subscript from crossing the border, the comparison time is saved.

② Average length of sequential searches when a successful query is successful:

In the case of equal probability, Pi = 1/N (1 ≤ I ≤ n), so the average length of successful searches is
(N +... + 2 + 1)/n = (n + 1)/2
That is, the average number of comparisons when the query is successful is about half of the table length.
If the K value is not in the table, the query fails only after N + 1 comparison.

③ ASL with an unequal search probability for each node in the table

Sequential search demo process
[Animation demonstration]

[Example] in the linear table composed of the School Students' medical records, the probability of finding the medical records of the physically weak and diseased students is higher than that of the healthy students... When P2. ≥p1, the minimum value is reached.
If you know in advance that the search probability of each node in the table is not equal and their distribution, you should store the nodes in the table from small to large based on the search probability to improve the efficiency of sequential search.
To improve the search efficiency, make the following modifications to the algorithm seqsearch: each time the search is successful, the found node and its successor (if any) node are exchanged. In this way, nodes with high search probability are constantly moved back during the search process, so as to reduce the number of comparisons in future searches.

④ Advantages of sequential search
The algorithm is simple and has no requirements on the table structure. It applies to both vector storage and linked list storage nodes, and whether the nodes are ordered by keywords or not.

⑤ Disadvantages of sequential search
The search efficiency is low. Therefore, it is not recommended to use sequential search when n is large.

Turn: Linear table search-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.