Class notes: search techniques for linear tables

Sequential search : Ordinary sequential search method, sequential search method with monitoring whistle
Half search : Decision tree for half search

#include  
using namespace std; 
const int MaxSize = 100; 
class LineSearch{ 
public:     
	LineSearch(int a[], int n);
	~LineSearch() {}
	int SeqSearch(int k);
	int BinSearch1(int k);
	int BinSearch2(int low, int high, int k);
private:     
	int data[MaxSize];
	int length;
};
LineSearch :: LineSearch(int a[ ], int n){     
	for (int i = 0; i < n; i++)         
		data[i+1] = a[i];
	length = n; 
}

The
basic idea of sequential search (linear search) : compare the key codes with the given value one by one from one end of the linear table to the other end, if they are equal, the search is successful, and the position of the record in the table is given; if the entire table is tested If the key code equal to the given value is still not found, the search fails and a failure message is given.

int LineSearch :: SeqSearch(int k) 
{         
	i=n;      
	while (i>0 && data[i]!=k)          
		i--;      
	return i; 
}

The
basic idea of improved sequence search : set up "Sentinel". The sentinel is the value to be checked. Putting the sentinel at the end of the search direction eliminates the need to judge whether the search position crosses the boundary after each comparison in the search process, thereby increasing the search speed.

int LineSearch :: SeqSearch(int k) 
{      
	int i = length;
	data[0] = k;
	while (data[i] != k)
		i--;     
	return i;  
}

Improvement method of sequential search and search performance
Record the access frequency of each data, and move the data with high access frequency to the right of the sequence table, which can reduce the number of comparisons made when the search is successful, improve the efficiency of
constructing an ordered sequence table, and reduce the search the number of comparisons performed fails to improve search efficiency
order to find a single list

int LinkSearch::SeqSearch2(Node *first, int k){    
	Node *p;  
	int count=0;
	p=first->next;   
	int j=1;
	while (p &&  p->data != k)  
	{
		p=p->next; 
		j++;    
		count++;
	}  
	if (!p){              
		cout<<"查找失败,比较的次数为:"<<count<<endl;                
		return 0;      
	} 
	else{      
		cout<<"\n"<<"查找成功,比较的次数为:"<<count<<endl;              
		return j;  
	} 
}

The advantages of sequential search : the
algorithm is simple and widely used.
There is no requirement for the storage structure of the records in the table, either sequential storage or linked storage;
there is no requirement for the orderliness of the records in the table, whether or not the records are ordered by key.
Disadvantages of sequential search : the
average search length is large, especially when there are many elements in the set to be searched, the search efficiency is low.
Binary search
Applicable conditions
: The records in the linear table must be ordered according to key codes; they must be stored in order.
Basic idea :
In an ordered list (low, high, low <= high), take the intermediate record as the comparison object, if the given value is equal to the key code of the intermediate record, the search is successful; if the given value is less than the intermediate record The key code is searched in the left half of the middle record; if the given value is greater than the key code in the middle record, the search is continued in the right half of the middle record. Repeat the above process continuously until the search is successful, or the searched area has no records, and the search fails.

int LineSearch :: BinSearch1(int k){      
	int mid, low = 1, high = length;
	while (low <= high) {
		mid = (low + high) / 2;            
		if (k < data[mid])                
			high = mid - 1;
		else if (k > data[mid])                 
			low = mid + 1;            
		else                
			return mid;
	}       
	return 0;
}
int LineSearch :: BinSearch2(int low, int high, int k){       
	if (low > high)            
		return 0;
	else {          
		int mid = (low + high) / 2;
		if (k < data[mid])             
			return BinSearch2(low, mid-1, k);       
		else if (k > data[mid])             
			return BinSearch2(mid+1, high, k);        
		else             
			return mid;
	} 
}

Determining the binary search tree
determination tree : binary search process can be described by a binary tree, each node of the tree corresponds to a record in the ordered list, the value of the node position recorded in the table. This binary tree describing the half-find search process is generally called a half-find search decision tree, or decision tree for short.
Decision tree construction method
⑴ When n = 0, the half-find decision tree is empty;
⑵ When n> 0, the root node of the half-find decision tree is mid = (n + 1) / 2, the left of the root node The subtree is a half-find decision tree corresponding to the ordered list r [1] ~ r [mid-1], and the right subtree of the root node is a half-half corresponding to r [mid + 1] ~ r [n] Find the decision tree.
The characteristics of the decision tree
Any two half-find search trees, if they have the same number of nodes, their structure is exactly the same
. The height of the half-finished search tree with n nodes is (log2n rounded down to +1).
The nature of the decision tree The
number of
nodes in the left and right subtrees of any node differs by at most 1 The height of the left and right subtrees of any node differs by at most 1 The level of
any two leaves differs by at most 1
fold and half Search performance analysis The
search is successful : in The process of searching for any record in the table is the path from the root node to the record node in the binary search decision tree, and the number of comparisons with the given value is equal to the number of layers of the record node in the tree.
Unsuccessful search : The process of searching for failure is to take a path from the root node to the external node, and the number of key comparisons with the given value is equal to the number of internal nodes on the path (average in case of failure) Find the length equal to the height of the tree).

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103358161