C + + Common search methods __c++

Source: Internet
Author: User

Search (search) Of course is also the most common operation, that is, in the data set to find the conditions of the data object, to find further to give the object of the specific information, in the database technology called Retrieval (retrieval).
the basis of the lookup

The lookup is based on whether the keyword (key word) is the same. The data that can be uniquely differentiated is referred to as the primary keyword .

such as student data, the data is a structural body variable;
struct student{
int id; Study number, can be used as the main key word
Char name[20]; Name
char sex; Gender
int age; Age
Char address[60]; Home Address
float eng, phy,math, Electron;
}; English, physics, maths, electronics, etc. can also be used as keywords

If the keywords are small in front, we are called ascending permutations, whereas in descending order.
Find The simplest lookup method--order lookup, starting with the first element of the array, and checking it in one order until the last element is found or discovered. When the data is in order, it can be used in half search (binary search). The algorithm performs more efficiently than sequential lookup. Hash Lookup: Hash (hash) lookup is the quickest way to find. Both of the lookup methods described above are for finding purposes by comparing the key values you want to find with the key values of the data elements in the table. If you can find a function f (key), convert the keyword to the position in the data table, and insert the data element directly into the position. A data element in a lookup that can be directly taken from that location. Such an organization is called a hash, and a hash lookup is called a hash lookup, which is a direct lookup. Also use transliteration to find the hash. Find Examples

Figure 6.3 and Figure 6.4 Describe how the semi lookup is done. Here is an ordered table in ascending order. (Demo lookup process)

Description: First arrange two pointer low and high point two elements, take mid= (Low+high)/2, such as mid point to the element is found, then end. If the element keyword is large, then take low=mid+1, high, continue to find, if the element keyword is small, then take high=mid-1,low unchanged, continue to find. If the Low>high is still not found, it fails and stops. Here is a step is very important, is low=mid+1 and high=mid-1, on the surface to see with Low=mid and high=mid instead of only less efficient, more than a mid to retain the data, but actually did not add 1 and minus 1 o'clock, the data may exist and can not be found. If in Figure 6.3, if found only 20, 21, 23 This step, then take Low=mid, then left 21, 23,mid = (low + high)/2, get mid = low, next low = Mid, or left 21, 23,mid can never point to 23, I'll never find 23. (Demo lookup process)

"Example 6.4" looks for a recursive algorithm, as an ordered Table template class member function. (View source)
Recursive method is easy to read and understand, but inefficient. Note that recursive implicit loop code is written.

"Example 6.5" finds the iterative algorithm in half. (view source) In this example, the iterative algorithm is not poor in readability and more efficient than recursion. Note The key point of the iteration's explicit loop code writing. * The member function BinarySearch (T & x), which appears in this example, is called the const member function, which guarantees read-only. The comparison operator for the corresponding node object overload must also be const.

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.