Static search tables: sequential search, half-lookup, and segmented search

Source: Internet
Author: User

Introduction:


Apart from various linear and non-linear data structures, there is also a data structure that is widely used in practical applications-query tables. A query table is a set of data elements of the same type.

The following operations are frequently 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. Only the first two operations collectively referred to as "Search" are performed on the query table, and such query tables are called static query tables. If you insert a data element that does not exist in the search table at the same time during the search process, or delete an existing data element from the search table, such a table is called a dynamic search table.


Basic knowledge:


The types of keywords and data elements are described as follows: typical types of keywords can be: typedef float keytype; // real-type typedef int keytype; // integer typedef char keytype; // string data element type is defined as: typedef struct {keytype key; // key field ........ // other domains} selmtype; macro definitions for the comparison conventions of the two keywords: // For the numeric keyword # define eq (A, B) (a) = (B )) # define LT (A, B) (a) <(B) # define SCSI (A, B) (a)> (B )) // string-type keyword # define eq (A, B )(! Strcmp (a), (B) # define LT (A, B) (strcmp (a), (B) <0) # define SCSI (, b) (strcmp (a), (B)> 0)

Specific analysis:


1. Sequential search.

Sequential search: 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.

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)

typedef int ElementType;#define EQ(a, b)  ((a) == (b))int sequential(int Array[], ElementType key, int n){int index;for(index = 0; index < n; index++){if(EQ(Array[index], key))return index + 1;}return -1;}

2. Half-lookup.

Half Lookup: A Half-fold query is also called a binary query. First, determine the range (range) of the record to be queried, and gradually narrow down the range until the record is found or cannot be found.

Applicability:It is highly efficient to search for large ordered tables. Suitable for tables that are rarely changed but frequently searched.

Advantages:

1. The half-fold search is more efficient than sequential search.

2. the time complexity of semi-query is log2 (n)

3. The average length of a half-fold query is log2 (n + 1)-1.

Disadvantages:

1. Semi-query is only applicable to ordered tables.

2. The half-fold query is limited to the sequential storage structure, and the linear linked list cannot be effectively searched.

The keyword key is compared with an array [I] element in the table, where 3 is the case:

1. Key = array [I], search successful

2. Key> array [I]. The possible range of the element to be searched is before array [I ].

3. Key <array [I]. The possible range of the element to be searched is after array [I ].

typedef int ElementType;#define EQ(a, b)  ((a) == (b))#define LT(a, b)  ((a) < (b))#define LQ(a, b)  ((a) <= (b))int Search_Bin(ElementType Array[], int num, int length){int index_low, index_mid, index_high;index_low = 1;index_high = length;while(index_low <= index_high){index_mid = (index_low + index_high) / 2;if(EQ(num, Array[index_mid]))return index_mid + 1;else if (LT(num, Array[index_mid]))index_high = index_mid - 1;elseindex_low = index_mid + 1;}return -1;}

3, Multipart search.

Multipart search: Block search, also known as index ordered search, is an improved method for sequential search. Divide n data elements into M Blocks (M <= N) by block order ). The data elements in each block do not need to be ordered, but the block and block must be "ordered by block", that is, the keywords of any element in the 1st fast must be less than the keywords of any element in the 2nd block; any element in the first block is smaller than any element in the second block ,......

Procedure:

1. Select the largest keywords in each quick step to form an index table.

2. Search is divided into two parts: Perform binary search or sequential search on the index table to determine the part of the record to be queried. Then, use the sequential method to search for the specified quick query.

Advantages: When you insert or delete a record in a table, you only need to find the block where the record is located, and then insert or delete the record in the block (due to fast unordered, so there is no need to move a large number of records ).

Disadvantages:Adds an auxiliary array storage space and performs block sorting on the initial table.

Performance:Between sequential search and binary search.

#define MAX 3#define MAXSIZE 18typedef int ElemType;typedef struct IndexItem{ElemType index;int start;int length;}IndexItem;IndexItem indexlist[MAX];ElemType MainList[MAXSIZE] = {22, 12, 13, 8, 9, 20, 33, 42, 44, 38, 24, 48, 60, 58, 74, 49, 86, 53};int sequential(IndexItem indexlist[], ElemType key){int index;if(indexlist[0].index >= key) return 1;for(index = 1; index <= MAX; index++){if((indexlist[index-1].index < key)&&(indexlist[index].index >= key))return index+1;}return 0;}int mainsequential(ElemType MainList[], int index, ElemType key){int i, num=0;for(i = 0; i < index-1; i++){num += indexlist[i].length;}for(i = num; i < num+indexlist[index-1].length; i++){if(MainList[i] == key) return i+1;}return -1;}

In addition to the three search methods described above, there are also the Fibonacci search and interpolation searches for ordered tables and the searches for static tree tables.

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.