作者在 2008-06-27 00:41:16 发布以下内容
// cfree 2008.6.10
#include "iostream"
#include "math.h"
using namespace std;
#define MAXSIZE 200
typedef int KeyType;
typedef int DataType;
typedef struct
{
KeyType key;
DataType data;
}NodeType;
typedef NodeType SeqList[MAXSIZE];
void SeqSearch(SeqList S,int n,KeyType k)
{
int i;
for(i=0;i<n&&S[i].key!=k;i++);
if(i<n)
{
cout<<"该数在第 "<<i+1<<" 位"<<endl;
cout<<"顺序查找次数为"<<i<<endl;
}
else
{
cout<<"The number is not exist!SeqSearch has searched "<<i<<" times"<<endl;
}
}
void BinSearch(SeqList S,int n,KeyType k)
{
int low=0,high=n-1,mid,num=0;
while(low<=high)
{
mid=(low+high)/2;
num++;
if(S[mid].key==k)
{
cout<<"折半查找次数为 "<<num<<endl;
exit(0);
}
else if(S[mid].key<k)
low=mid+1;
else
high=mid-1;
}
cout<<"The number is not exist!BinSearch has searched "<<num<<" times"<<endl;
}
void AreaSearch(SeqList S,int n,KeyType k)
{
int i,j,length=(int)sqrt(n);//分块的块长
bool check=false;//查到与否的标志
int b=n%length==0?0:1;
int num=n/length+b;//分的组数
for(i=0;i<num;i++)
{
int NUM=(i*num+num)<=n?(i*num+num):n;
if(k<S[NUM].key)
{
for(j=i*num;j<(NUM)&&k!=S[j].key;j++);
if(j<(NUM))
{
cout<<"分块查找次数为 "<<(i+j-i*num+1)<<endl;
check=true;
break;
}
else
{
cout<<"The number is not exist!AreaSearch has searched "<<(i+j-i*num+1)<<" times"<<endl;
check=true;
break;
}
}
else if(k==S[NUM].key)
{
cout<<"分块查找次数为 "<<i+1<<endl;
check=true;
break;
}
}
if(!check)
cout<<"没有找到!分块查找次数为 "<<i+1<<endl;
}
void Init(SeqList &Q,int &m,int &n)
{
while(m>MAXSIZE||m<0)
{
cout<<"请输入数组的长度(不要超过"<<MAXSIZE<<"):";
cin>>m;
}
cout<<endl;
for(unsigned i=0;i<m;i++)
{
Q[i].key=i;
cout<<Q[i].key<<" ";
if((i+1)%10==0)
cout<<endl;
}
cout<<endl<<"请输要查找的数据:";
cin>>n;
}
void main()
{
SeqList Q;
int n;//number you want to find
int m;//length of array
Init(Q,m,n);
SeqSearch(Q,m,n);
AreaSearch(Q,m,n);
BinSearch(Q,m,n);
}
#include "iostream"
#include "math.h"
using namespace std;
#define MAXSIZE 200
typedef int KeyType;
typedef int DataType;
typedef struct
{
KeyType key;
DataType data;
}NodeType;
typedef NodeType SeqList[MAXSIZE];
void SeqSearch(SeqList S,int n,KeyType k)
{
int i;
for(i=0;i<n&&S[i].key!=k;i++);
if(i<n)
{
cout<<"该数在第 "<<i+1<<" 位"<<endl;
cout<<"顺序查找次数为"<<i<<endl;
}
else
{
cout<<"The number is not exist!SeqSearch has searched "<<i<<" times"<<endl;
}
}
void BinSearch(SeqList S,int n,KeyType k)
{
int low=0,high=n-1,mid,num=0;
while(low<=high)
{
mid=(low+high)/2;
num++;
if(S[mid].key==k)
{
cout<<"折半查找次数为 "<<num<<endl;
exit(0);
}
else if(S[mid].key<k)
low=mid+1;
else
high=mid-1;
}
cout<<"The number is not exist!BinSearch has searched "<<num<<" times"<<endl;
}
void AreaSearch(SeqList S,int n,KeyType k)
{
int i,j,length=(int)sqrt(n);//分块的块长
bool check=false;//查到与否的标志
int b=n%length==0?0:1;
int num=n/length+b;//分的组数
for(i=0;i<num;i++)
{
int NUM=(i*num+num)<=n?(i*num+num):n;
if(k<S[NUM].key)
{
for(j=i*num;j<(NUM)&&k!=S[j].key;j++);
if(j<(NUM))
{
cout<<"分块查找次数为 "<<(i+j-i*num+1)<<endl;
check=true;
break;
}
else
{
cout<<"The number is not exist!AreaSearch has searched "<<(i+j-i*num+1)<<" times"<<endl;
check=true;
break;
}
}
else if(k==S[NUM].key)
{
cout<<"分块查找次数为 "<<i+1<<endl;
check=true;
break;
}
}
if(!check)
cout<<"没有找到!分块查找次数为 "<<i+1<<endl;
}
void Init(SeqList &Q,int &m,int &n)
{
while(m>MAXSIZE||m<0)
{
cout<<"请输入数组的长度(不要超过"<<MAXSIZE<<"):";
cin>>m;
}
cout<<endl;
for(unsigned i=0;i<m;i++)
{
Q[i].key=i;
cout<<Q[i].key<<" ";
if((i+1)%10==0)
cout<<endl;
}
cout<<endl<<"请输要查找的数据:";
cin>>n;
}
void main()
{
SeqList Q;
int n;//number you want to find
int m;//length of array
Init(Q,m,n);
SeqSearch(Q,m,n);
AreaSearch(Q,m,n);
BinSearch(Q,m,n);
}