苦逼的吉大孩子,你几班的啊,
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<fstream>
//#include<math>
using namespace std;
void search(int,int,int *,int *);
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>
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)//一个二叉树查找算法
{
icmp=0;
if(root==0)
{
icmp++;
if(itype==0)//itype=1时有插入功能(不同的值时)
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)//查找与插入
{
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 compare1(const void* a,const void* b)
{
return *(int *)a-*(int *)b;
}
int compare2(const void* a,const void* b)
{
return *(int *)b-*(int *)a;
} //主函数
int main()
{int p;
int m;
int n;
while(true)
{
cout<<"请选择数据类型 :1.正序排列2.逆序排列3.随机排列" <<endl;
cin>>p;
switch(p){
case 1 : {
FILE *fp;
char *filename="data.in";
if((fp=fopen(filename,"w+"))==NULL)
{
printf("cannot open this flie\n");
exit(0);
}
srand(time(0));
cout<<" 请输入数据规模n "<<endl;
cin>>n;
fprintf(fp,"\t\t%d\t",n);
cout<<" 请输入查找比较的操作次数 m "<<endl;cin>>m;
fprintf(fp,"\t\t%d\t\n",m);
int *a=new int[n];
for(int i=0;i<n;i++)
{ a[i]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",a[i]);
}
qsort(a,n,sizeof(int),compare1); //对数组进行升序排序
qsort(a,n,sizeof(int),compare2); //对数组进行降序排序
//随机生成m个随机数,并存入data.in
int *b=new int[m];
for(int k=0;k<m;k++)
{
b[k]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",b[k]);
}
fclose(fp);//关闭文件
search(m,n,a,b);
}
break;
case 2 : {
FILE *fp;
char *filename="data.in";
if((fp=fopen(filename,"w+"))==NULL)
{
printf("cannot open this flie\n");
exit(0);
}
srand(time(0));
cout<<" 请输入数据规模n "<<endl;
cin>>n;
fprintf(fp,"\t\t%d\t",n);
cout<<" 请输入查找比较的操作次数 m "<<endl;cin>>m;
fprintf(fp,"\t\t%d\t\n",m);
int *a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",a[i]);
}
qsort(a,n,sizeof(int),compare2); //对数组进行降序排序
//随机生成m个随机数,并存入data.in
int *b=new int[m];
for(int k=0;k<m;k++)
{
b[k]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",b[k]);
}
fclose(fp);//关闭文件
search(m,n,a,b);
}
break;
case 3 :
{
FILE *fp;
char *filename="data.in";
if((fp=fopen(filename,"w+"))==NULL)
{
printf("cannot open this flie\n");
exit(0);
}
srand(time(0));
//输入n,并存入data.in
//int n;
cout<<" 请输入数据规模n "<<endl;
cin>>n;
fprintf(fp,"\t\t%d\t",n);
//输入m,并存入data.in
cout<<" 请输入查找比较的操作次数 m "<<endl;
//int m;
cin>>m;
fprintf(fp,"\t\t%d\t\n",m);
//随机生成n个随机数,并存入data.in
int *a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",a[i]);
}
//随机生成m个随机数,并存入data.in
int *b=new int[m];
for(int k=0;k<m;k++)
{
b[k]=rand()%1000000001;
fprintf(fp,"\t\t%d\t\n",b[k]);
}
fclose(fp);//关闭文件
search(m,n,a,b);
}
}
}
return 0;
}//search函数
void search(int m,int n,int *a,int *b){
FILE *fp;
char *filename="data.out";
if((fp=fopen(filename,"w+"))==NULL)
{
printf("cannot open this flie\n");
exit(0);
}
clock_t start1 = clock();
for(int h=0;h<m;h++)
{
int key=b[h];
int icmp; int j=Find_s(a,n,key,icmp);//使用顺序查找法查找key值的位置 if(j==-1)
{
cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n顺序查找法:\n是否找到:"<<"no"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","no");
}
else
{
cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n顺序查找法:\n是否找到:"<<"yes"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","yes");
}
}
clock_t end1 = clock(); clock_t start2 = clock();
for(int f=0;f<m;f++)
{
int key=b[f];
int icmp;
Bintree<int> btree;
for(int i=0;i<n;i++)
btree.Find_bt(a[i],icmp,1);//未排序之前插入到二叉查找树中qsort(a,n,sizeof(int),compare1);//对数组进行升序排序 cout<<"二叉查找树:"<<endl;
int p = (btree.Find_bt(key,icmp)==NULL?-1:btree.Find_bt(key,icmp)->data );if(p==-1)
{
cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n二叉树查找法:\n是否找到:"<<"no"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","no");
}
else
{ cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n二叉树查找法:\n是否找到:"<<"no"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","no");} } clock_t end2 = clock(); clock_t start3 = clock(); for(int g=0;g<m;g++)
{
int key=b[g];
int icmp;
Hash<int > hash(1000);
hash.init_hash(a,n);cout<<"哈希表查找:"<<endl;
int q = ((hash.Find_hash(key,icmp)==NULL)?-1:(hash.Find_hash(key,icmp))->data);if(q==-1){
cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n哈希查找法:\n是否找到:"<<"no"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","no");
}
else{ cout<<"数据总数:"<<n<<"\t查找关健字key:"<<key<<
"\t查找比较次数:icmp\n二叉树查找法:\n是否找到:"<<"yes"<<" icmp:"<<icmp<<endl;
fprintf(fp,"\t\t%s\t\n","yes");
} } clock_t end3 = clock();
cout<<"顺序查找时间 :"<<(double)(end1-start1)/CLOCKS_PER_SEC<<endl<<endl;//输出时间
cout<<"二叉树查找时间 :"<<(double)(end2-start2)/CLOCKS_PER_SEC<<endl<<endl;//输出时间
cout<<"哈希查找时间 :"<<(double)(end3-start3)/CLOCKS_PER_SEC<<endl<<endl;//输出时间
fclose(fp);//关闭文件
}
不过 运行结果 不能排序 只能是随机的,你看看能改好吗?改好了 说给我 我11级12班的