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

Source: Internet
Author: User

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

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)





How does an average search algorithm in a static search algorithm use C language ??? Urgent

There are two common static search algorithms:

1. Sequential search. The average search length is (n + 1)/2.

2. Semi-query of an ordered table: the premise must be that an ordered table has the optimal performance only when the table is evenly distributed.

Average search length: log2 (n + 1)-1
Algorithm Implementation:
1. Sequential search

Search from the first element of the array until the location of the element to be searched is found until the result is found.

The best condition is 1, and the worst condition is O (n ).

# Include "stdio. h"
# Define maxitem100
Typedef int KeyType;
Typedef int ElemType;
Typedef struct
{
KeyType key;
ElemType data;
} NodeType;
Typedef NodeType SeqList [MAXITEM];/* sequence table type */

// Algorithm sequential search
Int SeqSearch (SeqList R, int n, KeyType k)
{
Int I = 0;
While (I <n & R [I]. key! = K)/* locate from the header */
I ++;
If (I> = n)
Return-1;
Else
Return I;
}

Void main ()
{
Int I, n = 10;
SeqList R;
KeyType a [] = {2, 3, 5, 4, 9, 0, 7, 6}, x = 9;
For (I = 0; I <n; I ++)
R [I]. key = a [I];
Printf ("R [% d] = % d \ n", SeqSearch (R, n, x), x );
}

2. Binary Search

Semi-query continuously divides the elements of the array to be searched into two parts. Each time 1/2 is eliminated, the main premise is that the elements must be ordered, if it is unordered, sort it first.

The best condition is 1, and the worst query status is O (n)

# Include "stdio. h"
# Define maxitem100
Typedef int KeyType;
Typedef int ElemType;
Typedef struct
{
KeyType key;
ElemType data;
} NodeType;
Typedef NodeType SeqList [MAXITEM];/* sequence table type */

// Algorithm Binary Search
Int BinSearch (SeqList R, int n, KeyType k)
{
Int low = 0, high = n-1, mid;
While (low <= high)
{
Mid = (low + high)/2;
If (R [mid]. key = k)/* returns if the search is successful */
Return mid;
If (R [mid]. key> k)/* continue searching in R [low .. mid-1 */
High = mid-1;
Else
Low = mid + 1;/* continue searching in R [mid + 1 .. high */
}
Return-1;
}

Void main ()
{
Int I, n = 10;
SeqList R;
KeyType a [] = {2, 13, 15 ...... the remaining full text>

Use C language to compile an algorithm for searching a static search sequence table

# Include <stdio. h>
/* ++
The type can be determined based on actual conditions.
++ */
Int Lsearch (int * L, int n, int key) // if find return the location
// If not return-1;
{
Int I;
For (I = n-1; I> = 0; I --) if (key = L [I]) return I;
Return-1;
}
Int Bsearch (int * L, int n, int key) // if find return the location
// If not return-1
{
Int l = 0, r = n-1, mid;
While (l <= r)
{
Mid = (l + r)/2;
If (L [mid] = key) return mid;
Else if (L [mid] <key) l = mid + 1;
Else r = mid-1;
}
Return-1;
}
Int main ()
{
Int a [6] = {
1, 2, 3, 4, 5, 6
};
Printf ("Lsearch: \ n ");
Printf ("find % d \ n", Lsearch (a, 6, 4 ));
Printf ("not find % d \ n", Lsearch (a, 6, 7 ));
Printf ("Bsearch: \ n ");
Printf ("find % d \ n", Bsearch (a, 6, 4 ));
Printf ("not find % d \ n", Bsearch (a, 6, 7 ));
}
 

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.