Static lookup Table
Lookup definition: Given a value k, in a table (or file) containing n nodes, find the node with the key equal to the given value K, if found, the lookup succeeds, the location of the node in the table is output, otherwise the lookup fails, and the output finds the failed information.
Features: Only search, not change the data elements in the lookup table (such as the college Entrance Examination results table, the staff information table, etc.); one: Sequential lookup
Lookup procedure: For a given keyword K, starting at one end of a linear table (a sequential table or a linear linked list), record the comparison of the keyword and k, until a record of the keyword equals k is found or the other end of the table is reached. The key value is usually stored in unit No. 0 of the linear table as a "sentinel".
Watch the whistle : avoid every step of the way to determine whether or not to cross.
-Advantages: The algorithm is simple, no sorting, sequential and chained storage can be used.
-Cons: Average lookup length is large.
-Average search Length:
Find success: Asl=∑i=1npici=1n∑i=1nci=1n∗n (n+1) 2=n+12 Find success: ASL = \sum_{i=1}^n p_i c_i = \frac{1}{n} \sum_{i=1}^n C_i = \frac{1}{n} * \frac{n (n+1)}{2}= \frac{n+1}{2}
Lookup failed: Asl=n+1 lookup failed: ASL = n + 1
N: Number of records in table
Pi: Finding the probability of the first I record
Ci: The number of comparisons that need to be made to find the first I record
#include <iostream>
using namespace std;
/**
* @param args *//in order to
find the data element whose keyword equals key in sequential table St, if found, the function value is the position of the element in the table, otherwise returns 0
static int searchseq (int st[], int key,int len) {//st Table No. 0 units do not use
//from backward looking, and set "Sentinel" in unit No. 0
//int len = sizeof (ST)/sizeof (st[0]) -1;//This is wrong, The resulting result is always 1
st[0] = key;//set Watch (Sentinel)
while (St[len]! = key) {
len--;
}
return len;
}
int main (void) {
int st[] = {0, 10, 20, 40, 80, 30, 60, 25};//0 unit not used ;
int len = sizeof (ST)/sizeof (St[0]) The -1;//array occupies the total memory space, divided by the size of a single element in memory,
cout<< "array length:" <<len<<endl;
int key = 0;
cin>>key;
int result = Searchseq (St, Key,len);
if (Result > 0)
cout<< "Find" <<key<< "successful, position in lookup table is" <<result<<endl;
else
cout<< "Find" <<key<< "failed" <<endl;
System ("pause");
return 0;
}
II: Binary lookup (lookup of ordered tables)
Prerequisites: Must be done in an ordered table with a sequential storage structure.
Find ideas: Determine the scope of the record you want to find, and then gradually narrow it down until you find or confirm that the record is not found.
#include <iostream> using namespace std;
In the ordered table St, binary finds the data element whose key is equal to key, and if found, the function value is the position of the element in the table, otherwise returns 0 static int searchseq (int st[], int key,int len) {//st Table No. 0 units not used
int len = sizeof (ST)/sizeof (st[0]) -1;//This is wrong, the resulting result is always 1 int low = 1, high = len-1;
while (low <= high) {int mid = (low + high)/2;//Take intermediate value if (st[mid] = = key) {return mid;
}else if (St[mid] > key) high = mid-1;
else//st[mid] < key low = mid + 1;
} return 0; } int main (void) {///binary lookup test for ordered table int st[] = {0, 8, 17, 25, 44, 68, 77, 98, 100, 115, 125};//0 unit does not use int l
En = sizeof (ST)/sizeof (st[0]);//array occupies total memory space, divided by the size of a single element in memory space, cout<< "array length:" <<len<<endl;
int key = 0;
cin>>key;
int result = Searchseq (St, Key,len);
if (Result > 0) cout<< "Find" <<key<< "successful, position in lookup table is" <<result<<endl; Else cout<< "Find" <<key<< "failed" <<endl;
System ("pause");
return 0; }
Algorithm Analysis:
In order to analyze the performance of binary search, binary tree can be used to describe the binary search process. The midpoint of the current search interval is used as the root node, the left sub-interval and the right sub-interval respectively as the root of the Saozi right subtree, the left sub-range and the right sub-range and then a similar method, resulting in a two-fork tree called two-point search decision tree.
Example: Decision tree for keyword sequence {8,17,25,34,48,57,68}
The K-layer nodes are searched for K-times (the root node is the 1th layer), and the K-layer node number is 2^k-1. Assuming that the two fork tree depth is h, then equal probability, the binary finds the successful ASL as:
Asl=∑i=1npici=1n∑i=1nci=