几种常用查找算法(顺序查找法、二分法、二叉树、哈希表法)的比较 // 临时工作区.cpp : Defines the entry point for the console application. //2008.9.8 数据结构课程设计第一至二题 #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; template<class T> int Find_s(T data[], int n,T key,int &icmp) //顺序查找(从n维数组中查找key,并且给出比较的次数icmp { icmp=0; for(int i=0;i<n;i++) { icmp++; if(data[i]==key) return i; } return -1; } template<class T> int Find_t(T data[], int n,T key,int &icmp) //二分查找(从n维数组中查找key,并且给出比较的次数icmp { int i=0,j=n-1; icmp=0; while(data[(i+j)/2]!=key&&i<=j) { icmp++; if(data[(i+j)/2]>key) j=(i+j)/2-1; else i=(i+j)/2+1; } if(i>j) return -1; return (i+j)/2; } ///以下是二叉查找树查找法 template<class T> class BintreeNode { public: T data; BintreeNode* left; BintreeNode*right; BintreeNode():left(0),right(NULL){} BintreeNode(T item):data(item),left(NULL),right(NULL){} ~BintreeNode(){ if(left!=0) delete left; if(right!=0) delete right; } }; template<class T> class Bintree { public: int num; BintreeNode<T>* root; BintreeNode<T>* Find_bt(T key,int &icmp,int itype=0)//一个二叉树查找算法,itype=1时有插入功能(不同的值时) { icmp=0; if(root==0) { icmp++; if(itype==0) return NULL; else { num++; return root=new BintreeNode<T>(key); } } else { BintreeNode<T>* ptr=root,*p; while(ptr!=NULL) { icmp++; p=ptr; if(ptr->data==key) return ptr; else { if(ptr->data>key) ptr=ptr->left; else ptr=ptr->right; } } if(itype) { num++; if(p->data>key) return p->left=new BintreeNode<T>(key); else return p->right=new (BintreeNode<T>)(key); } return NULL; }//else }//Find_bt Bintree():root(0),num(0){} ~Bintree(){delete root;} }; int compare(const void* a,const void* b) { return *(int *)a-*(int *)b; } template<class T> class Link { public: T data; Link<T>* next; Link<T>():next(0){} Link<T>(T item):data(item),next(0){} ~Link<T>(){if(next) delete next;} }; template<class T> class LinkList { public: Link<T>* first; LinkList<T>():first(0){} ~LinkList<T>(){if(first)delete first;} Link<T>* Find_hash(T key,int &icmp,int ntype=0)//查找与插入,当ntype=1时为插入 { icmp=0; if(first==0) { icmp++; if(ntype) return first=new Link<T>(key); else return NULL; } else { Link<T>*ptr=first,*p; while(ptr!=NULL) { icmp++; p=ptr; if(ptr->data==key) return ptr; ptr=ptr->next; } if(ntype) return p->next=new Link<T>(key); return NULL; } } }; template<class T> class Hash { public: LinkList<T>* hashlist; int size; Hash<T>(int n=113):size(n) { hashlist=new LinkList<T>[n]; } ~Hash<T>(){delete []hashlist;} void init_hash(T data[],int n)//初始化哈希查找表-----拉链法冲突调节 { int t; for(int i=0;i<n;i++) { int pos=data[i]%size; hashlist[pos].Find_hash(data[i],t,1); } } Link<T>* Find_hash(T key,int &icmp)//查找关键词 采用除法杂凑函数 { int pos=key%size; return hashlist[pos].Find_hash(key,icmp); } }; int main() { srand(time(0)); do{ int n=rand()%100000+1000; int *a=new int[n]; for(int i=0;i<n;i++) a[i]=rand()%100000; int key=rand()%n; int icmp; int j=Find_s(a,n,key,icmp);//使用顺序查找法查找key值的位置 cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<< "\t查找比较次数:icmp\n顺序查找法:\npos:" <<j<<"\tdata:"<<a[j]<<" icmp:"<<icmp<<endl; Bintree<int> btree; for(i=0;i<n;i++) btree.Find_bt(a[i],icmp,1);//未排序之前插入到二叉查找树中 qsort(a,n,sizeof(int),compare);//对数组进行升序排序 cout<<"二分查找法:"<<endl; cout<<"pos:"<<Find_t(a,n,key,icmp)<<"\ticmp:";//使用二分查找法查找key值的位置 cout<<icmp<<endl; cout<<"二叉查找树:"<<endl; cout<<"data:"<<(btree.Find_bt(key,icmp)==NULL?-1:btree.Find_bt(key,icmp)->data ); cout<<"\ticmp:"<<icmp<<endl;//"\tnum:"<<btree.num<<endl; Hash<int > hash(1000); hash.init_hash(a,n); cout<<"哈希表查找:"<<endl; cout<<"data:"<<((hash.Find_hash(key,icmp)==NULL)?-1:(hash.Find_hash(key,icmp))->data); cout<<"\ticmp:"<<icmp<<endl; } while(getchar()-'0');//直到输入为'0'时结束,一直按Enter可以继续进行运算 return 0; }