Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Search

This article mainly discusses various methods for searching ordered tables, ordered tables, index tables, and hash tables, as well as the average length of the corresponding searching methods in the case of equal probability.

Search Table: a collection of data elements (objects) of the same type. Each element is usually composed of several data items. Keyword (Key, code): the value of A (or several) data item in a data element. It can identify a data element. If a keyword uniquely identifies a data element, the Key is called the Primary Key. The Key that identifies several data elements is called the Secondary Key ). Searching: determines the record or data element in the query table where a keyword is equal to the given value based on the given K value.

◆ There are records in the search table that meet the conditions: Query successful; Result: the information or position of the records found in the search table.
◆ No matching record exists in the search table: search failed.
According to the operation method of searching a table, it can be divided into static searching table and Dynamic Searching table.
Static Search: performs the following operations only on the table:
(1) check whether a specific data element is in the query table,
(2) retrieve a specific element and various attributes.
Dynamic Search: You can perform the following operations during the Search process:
(1) you can insert data elements that do not exist in the query table,
(2) Delete an existing record from the search table.
To improve search efficiency, in addition to applying good search algorithms, a special data structure will be set up for search to store data, such as tables and trees.
Query the storage structure of a table:
Table searching is a flexible data structure that can be stored in multiple ways.
Based on different storage structures, the search methods can be divided into three categories:
① Search for ordered tables and linked lists: Compare the given K value with the keyword recorded in the search table to find the record to be searched;
② Search for a hash: directly access the query table based on the given K value to find the record to be searched;
③ Search for an index: first, identify the block of the record to be searched based on the index, and then find the record to be searched from the block.
Generally, we think that the record keywords are some types that can be compared, such as integer, struct, and real, the following sections describe the types of keywords and data elements involved in this chapter:
Typical keyword types are described as follows:

Typedef float KeyType;/* real type */typedef int KeyType;/* integer */typedef char KeyType;/* Quiet type */Data Element type is defined: typedef struct RecType {KeyType key;/* keyword Code */keywords/* Other domains */} RecType;
Static search
ABSTRACT Data Types of static search tables are defined as follows:

ADT Static_SearchTable {
Data Object D: D is a set of data elements with the same features,
Each data element is a key word with a unique identifier.
Data Relationship R: data elements belong to the same set.
Basic operation P:
Create
Destroy
Search
Traverse
} ADT Static_SearchTable
A linear table is the simplest way to query a table. This section describes several main methods for finding a linear table.

Sequential search

Applicable scenarios: the elements in the search table are not sorted by keywords (I .e., unordered linear table)
1. Search for ideas
Compare the key words of a record with the given K value one by one from one end of the table (the first or the last one). If the key word of a record is equal to the given K value, the query is successful. Otherwise, if no corresponding record is found for the entire table, the search fails. Sequence Table types are defined as follows:

# Define MAX_SIZE 100 typedef struct SSTable {RecType elem [MAX_SIZE];/* sequence table */int length;/* actual number of elements */} SSTable;
Int Seq_Search (SSTable ST, KeyType key) {int I; ST. elem [0]. key = key;/* set the monitoring Sentinel. If the monitoring fails, 0 */for (I = ST. length; I> = 1; I --) if (ST. elem [I]. key = key) return I; return 0 ;}
Half Lookup

Prerequisites: all records in the search table are sorted by keywords in ascending or descending order ).
In the search process, first determine the range of the records to be queried in the table, and then gradually narrow down the range (half the number of records to be queried each time) until the records are found or cannot be found.
1. Search for ideas
Use Low, High, and Mid to indicate the lower bound, upper bound, and middle position pointer of the interval to be searched. The initial values are Low = 1 and High = n.
(1) take the middle position Mid: Mid = (Low + High)/2;
(2) Compare the keyword recorded in the intermediate position with the given K value:
① Equal: the search is successful;
② Greater than: the records to be queried are in the first half of the interval. Modify the upper bound pointer: High = Mid-1, and convert it to (1;
③ Less than: the records to be queried are in the second half of the interval. Modify the lower bound pointer: Low = Mid + 1, and convert it to (1;
The query fails until the request is out of bounds (Low> High.

2 Algorithm Implementation int Bin_Search (SSTable ST, KeyType key) {int Low = 1, High = ST. length, Mid; while (Low <High) {Mid = (Low + High)/2; if (ST. elem [Mid]. key = key) return (Mid); else if (ST. elem [Mid]. key <key) Low = Mid + 1; else High = Mid-1;} return (0);/* search failed */}
Fibonacci search
The Fibonacci search method is to split the search table based on the characteristics of the Fibonacci series. F (0) = 0, F (1) = 1, F (j) = F (J-1) + F (J-2 ). If a total of 20 (F (8)-1) numbers, j = 8, then mid = low + F (7)-1 = 13 if key <a [13], then low = 1, high = 12 next: j = 7, mid = low + F (6)-1 = 8 if key> a [13], then low = 14, high = 20 (F (8)-1) next step: j = 6, mid = low + F (5)-1 = 18

1. Search for ideas
Set the number of records in the query table to 1 smaller than the number of records in a certain Fibonacci, that is, set n = F (j)-1. Use Low, High, and Mid to indicate the lower bound, upper bound, and split position of the interval to be searched. The initial values are Low = 1 and High = n.
(1) take the split position Mid: Mid = low + F (J-1)-1;
(2) Compare the keyword of the split location record with the given K value:
① Equal: the search is successful;
② Greater than: To be queried records in the first half of the range (Interval Length is F (J-1)-1), modify the upper bound pointer: High = Mid-1, to (1;
③ Less than: To be queried records in the second half of the range (Interval Length is F (J-2)-1), modify the lower bound pointer: Low = Mid + 1, turn (1;
The query fails until the request is out of bounds (Low> High.
Algorithm Implementation
To avoid frequent calculation of the number of Fibonacci during algorithm implementation, two variables f1 and f2 can be used to save the number of adjacent two Fibonacci numbers. In this way, we can calculate them in sequence in future computation.

Int fib (int n) {int I, f, f0 = 0, f1 = 1; if (n = 0) return (0); if (n = 1) return (1); for (I = 2; I <= n; I ++) {f = f0 + f1; f0 = f1; f1 = f ;} return (f);} int Fib_search (RecType ST [], KeyType key, int n) /* use the Fibonacci method in the ST of the ordered table to search for records with the key keyword */{int Low = 1, High, Mid, f1, f2; High = fib (n)-1; f1 = fib (n-1); f2 = fib (n-2); while (Low <= High) {Mid = Low + f1-1; if (ST. [Mid]. key = key) return (Mid); else if (key <ST. [Mid]. key) {High = Mid-1; f2 = F1-F2; f1 = F1-F2;} else {Low = Mid + 1; f1 = F1-F2; f2 = f2-f1 ;}} return (0 );}

The time complexity of fiber ACCI search is also O (logn)
Average performance, better than half-lookup; but in the worst case, the performance is worse than half-lookup.
Addition and Division operations are required for the half-fold search. However, only the addition and subtraction operations are required for the Fibonacci search.

Multipart search

Blocking Search, also known as index sequential Search, is a combination of the first two Search methods.
1. query the organization of a table
① Divide the search table into several parts. Block order, that is, all record keywords in block I + 1 are greater than (or less than) block I record keywords; block unordered.
② Add an index table to the search table. The index table is ordered by keywords, and the record structure in the index table is

Application
Multipart indexes greatly increase the overall search speed when the segments do not need to be ordered. They are widely used in Database Table search technology.

The algorithm implements typedef struct IndexType {keyType maxkey;/* the largest keyword in the block */int startpos;/* the starting position of the block */} Index; int Block_search (RecType ST [], index ind [], KeyType key, int n, int B)/* query records with the key keyword in the partitioned Index * // * The table length is n, the number of blocks is B */{int I = 0, j, k; while (I <B) & LT (ind [I]. maxkey, key) I ++; if (I> B) {printf ("\ nNot found"); return (0) ;} j = ind [I]. startpos; while (j <n) & SCSI (ST [j]. key, ind [I]. maxkey) {if (EQ (ST [j]. k Ey, key) break; j ++;}/* find in the block */if (j> n |! EQ (ST [j]. key, key) {j = 0; printf ("\ nNot found") ;}return (j );}

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.