Search Algorithm for algorithm learning: static search table (1) sequential table search

Source: Internet
Author: User

Search Algorithm for algorithm learning: static search table (1) sequential table search

Introduction:


The following operations are generally performed on a query table: 1. query whether a "specific" data element is in the query table; 2. query the attributes of a "specific" data element; 3. Insert a data element into the search table; 4. delete a data element from the search table.


There are only two types of static table search operations: 1. query whether a "specific" data element is in the search table; 2. query the attributes of a "specific" data element;


There are four forms of static search tables: Search for ordered tables, search for ordered tables, search for static trees, and search for indexed ordered tables.


The keyword types and data element types involved in static search are described as follows:

/* Typical keyword type */typedef float KeyType; // real typedef int KeyType; // integer typedef char * KeyType; // string type/* Data Element type */typedef struct {KeyType key; // keyword field ..... // other domains} SElemType;/* The comparison conventions for the two keywords are as follows * // ----- for the numeric keyword # define EQ (a, B) () = (B) # define LT (a, B) (a) <(B) # define SCSI (a, B) (a)> (B )) // ----- keyword # define EQ (a, B )(! Strcmp (a), (B) # define LT (a, B) (strcmp (a), (B) <0) # define SCSI (, b) (strcmp (a), (B) <= 0)


The sequence table structure is defined as follows:

Typedef struct {

ElemType * elem; // The base address of the data element storage space. The base address is allocated according to the actual length during table creation. No value is left for unit 0.

Int length; // table length

} SSTable;


Sequential search process: Start from the last record in the table and compare the record keywords with the given values one by one. If the keywords of a record are equal to the given values, the query is successful and the queried records are found. Otherwise, if the keywords of the first record are not the same as the given value, it indicates that no records are found in the table and the query is unsuccessful.


Assume that the length of the sequence table is 10, that is, length = 11. elem indicates that each data is 10 25 36 49 52 15 68 45 90 80. The following figure shows the sequence query:


Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

'\ 0' 10 25 36 49 52 15 68 45 90 80


The number 0 unit of elem is empty. Assume that the search element is in the sequence table, for example, to search for 36, first put 36 into the number 0 unit of elem, and then compare it from 80 in sequence, until 36 in Unit 3 is met, the search is successful. Example:


Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

36 10 25 36 49 52 15 68 45 90 80 // place 36 in unit 0 as a sentry


Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

36 10 25 36 49 52 15 68 45 90 80 // compare 36 with 80, then proceed


Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

36 10 25 36 49 52 15 68 45 90 80 // compare 36 with 90, then proceed

.........

Move the comparison element forward sequentially. When the comparison result is equal to the value of 3, the comparison ends.

Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

36 10 25 36 49 52 15 68 45 90 80 // compare 36 with 36, equal, end of comparison, return I

If the value to be compared is not in the sequence table, for example, if the given value is 5, first place 5 to unit 0,

Elem ---> [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

5 10 25 36 49 52 15 68 45 90 80 // place 5 in unit 0 as a sentry

When it is compared to 5, the returned I value is 0. Failed to search.


Implement the sequential storage structure of static search tables:

Sample Code (C language description ):

/*************************************** * ***************************** Author: li Bing date: 2014-9-20Email: *************************************** * ****************************/# define EQ (, b) (a) = (B) # defineElemTypeinttypedef struct {ElemType * elem; // The base address of the data element bucket, which is allocated according to the actual length during table creation, unit 0 left an int length; // table length} SSTable; // search for data elements whose keywords are key in sequence in the sequence table ST. If found, the function value is the position of the element in the table; otherwise, it is 0int Search_Seq (SSTable ST, ElemType key) {int I; ST. elem [0] = key; // "Sentry" for (I = ST. length ;! EQ (ST. elem [I], key); -- I); // return I from the back. // when I cannot be found, I is 0}



Performance Analysis: We know that when we discuss the performance of a program, we generally take three perspectives: time complexity, space complexity, and other performance of algorithms. In the search process, we usually only need a fixed size of auxiliary space for comparison, so the space complexity is certain. The time complexity is variable: the average value of the number of records whose keywords are compared with the given values.

Applicability: Sequential search is generally applicable when there is a small amount of data.

Advantages:

1. Simple Algorithm Implementation and wide adaptability

2. There is no requirement on the structure of the table, regardless of whether the records are sorted by keywords.

3. It is applicable to ordered tables and single-chain tables.

Disadvantages:

1. The average search length is large, especially when n is large, the search efficiency is low.

2. Slow speed. The average search length is (n + 1)/2, and the time complexity is O (n)




Related Article

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.