14. Search Overview and sequential search, and 14 Search Overview order

Source: Internet
Author: User

14. Search Overview and sequential search, and 14 Search Overview order
1. Search Overview1. Search:That is, a data element (or record) whose keyword is equal to the given value is determined in the search table based on a given value ). If the table does not have a record with the keyword equal to the given value, it is said that the query is unsuccessful. At this time, the search result can provide a "null" record or "null" pointer. In addition, there are two ways to search tables: static and dynamic.(1) Search table): A set of data elements (or records) of the same type;(2) Key)It is the value of a data item in a data element, also known as a key value. It can be used to mark a data element or a data item (field) of a record, also known as a key code;(3) Primary keywords:If this keyword can uniquely identify a record, it is called as the Primary Key. The data item where the Primary Key is located is the Primary Key;(4) Secondary keyword (Secondary Key): Key words that can recognize multiple data elements (or records) are called secondary keywords;
2. Static Search Table): Only search tables. Its main operations include: (1) querying whether a "specific" data element is in a search table; (2) retrieving a "specific" Data Element and various attributes.3. Dynamic Search Table)Insert a data element that does not exist in the search table during the search process, or delete an existing data element from the search table. Its main operations include: (1) Inserting data elements during search; (2) Deleting element data elements during search; Note: to improve search efficiency, we need to set up a data structure specifically for the search operation, that is, to change the relationship between data elements (tables, trees, and other structures ).
Ii. Sequential table search1. Definition: Sequential lookup, also known as linear lookup, is the most basic lookup technique. Its search process starts with the first (or last) record in the table, compare the record keywords with the given values one by one. If the keywords of a record are the same as the given values, the query is successful and the queried records are found, when the keyword and the given value are both different, no records are found in the table and the query fails.2. Sequential table Search Algorithm/* Sequential search: a is an array, n is the number of arrays to be searched, and key is the keyword */int Sequential_Search (int * a, int n, int key) {int I; for (I = 1; I <n; I ++) {if (a [I] = key) return I; // return the record location of the keyword }}3. Sequential table Search OptimizationGenerally, the sequential search algorithm must judge whether I is less than or equal to n in each loop. In some programs, when the total data volume is large, this judgment will undoubtedly reduce the search efficiency. We can set a "Sentinel" at the end to solve the problem of comparing I and n each time to improve the search efficiency. Advantages: simple implementation and high efficiency of small data query. Disadvantage: WHEN n is large, the search efficiency is very low. /* Search for the X/int Sequential_Search2 (int * a, int n, int key) {int I; a [0] = key; // set a [0] As the keyword value, which is called "Sentinel" I = n; // The loop starts from the end of the array while (a [I] ~ = Key) {I --;} return I; // If 0 is returned, the search fails}Analysis: This code starts searching from the end. If there is a key in a [I], it returns the I value and returns the result. Otherwise, it must be equal to the key at the final a [0, at this time, 0 is returned, indicating a [1] ~ No key keyword in a [n]. Search failed. Time Complexity: The best case is O (1), the worst case is O (n); the time complexity of failure n + 1 query is O (n ); the average number of searches is (n + 1)/2, that is, the average complexity is O (n ).

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.