Find Concepts
Lookup table: A collection of data elements of the same type.
Look-up tables are divided into two main ways: static lookup tables and dynamic lookup tables.
Static lookup table: A lookup table that is used only for lookup operations, and its main operations are:
Find a "specific data element"
Dynamic lookup Table: Inserts a data element that does not exist in the lookup table during the lookup process, or deletes a data element that already exists from the lookup table. To dynamically find a table operation:
Find when inserting data and deleting data while looking
For static lookup tables, you can apply a linear table structure to organize your data so that you can use sequential lookup algorithms. If you sort the primary key again, you can apply techniques such as binary find to find it efficiently.
For dynamic lookups, consider a two-fork sort tree lookup technique.
Static Table Lookup
Sequential Table Lookup
Sequential lookup: Starting with the first (or last) record in a table, a record-by-keyword comparison with a given value, if a record's keyword is equal to the given value, the lookup succeeds. If the keyword and the given value comparisons are not equal until the last (or first) record, the lookup is unsuccessful.
int sequential_search (int *a, int n, int key) {int i;for (i = 1; I <= n; i++) {if (a[i] = = key) {return i;}} Forreturn 0;}
However, this algorithm can be improved by seeing that each element is moved backwards to compare the current number of indexes and arrays to prevent cross-border. In fact, you can set up a sentinel so that you don't have to compare I to n every time.
/* have Sentinel order lookup, find successful return subscript, unsuccessful return 0*/int sequential_search2 (int *a, int n, int key) {int i = n;a[0] = key;//set a[0] As the keyword, called Sentinel while (a [i]! = key) {i--;} return i;}
This method of setting the Sentinel at the end of the lookup direction avoids the need to determine whether the lookup location is out of bounds after each comparison in the lookup process. When the amount of data is high, the efficiency is greatly improved, and it is a very good programming skill.
Binary find
In an ordered table , the intermediate record is taken as the comparison object, and if the given value is equal to the intermediate record, the lookup succeeds. If the given value is less than the intermediate record, the left half of the intermediate record continues to be searched, and if the given value is greater than the intermediate record, it is found in the right half.
[Data structure] Lookup