#include <iostream.h>
#include <malloc.h>
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SSTable;
bool Create(SSTable &ST)
{
int n,i;
cout<<"please input the mount the data you want to order:";
cin>>n;
ST.elem=(ElemType *)malloc((n+1)*sizeof(ElemType));
if (!ST.elem) return false;
ST.length=n;
cout<<"please input the data:"<<endl;
for(i=1;i<=n;i++)
{
cin>>ST.elem[i];
}
return true;
}
int Search(SSTable ST,ElemType key)
{
int i;
ST.elem[0]=key;
i=ST.length;
while(ST.elem[i]!=key)
{
--i;
}
return i;
}
int Search_Bin(SSTable ST,ElemType key)
{
int low,mid,high;
low=1;
high=ST.length;
while(low<=high)
{
mid=(low+high)/2;
if(ST.elem[mid]==key) return mid;
else if(ST.elem[mid]>key) high=mid-1;
else low=mid+1;
}
return 0;
}
void main()
{
int m;
SSTable ST;
Create(ST);
cout<<"please input the data that you want to search:"<<endl;
cin>>m;
cout<<"the number that you want to search is at the "<<Search(ST,m)<<endl;
cout<<"the number that you want to search is at the "<<Search_Bin(ST,m)<<endl;
}
#include <malloc.h>
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
}SSTable;
bool Create(SSTable &ST)
{
int n,i;
cout<<"please input the mount the data you want to order:";
cin>>n;
ST.elem=(ElemType *)malloc((n+1)*sizeof(ElemType));
if (!ST.elem) return false;
ST.length=n;
cout<<"please input the data:"<<endl;
for(i=1;i<=n;i++)
{
cin>>ST.elem[i];
}
return true;
}
int Search(SSTable ST,ElemType key)
{
int i;
ST.elem[0]=key;
i=ST.length;
while(ST.elem[i]!=key)
{
--i;
}
return i;
}
int Search_Bin(SSTable ST,ElemType key)
{
int low,mid,high;
low=1;
high=ST.length;
while(low<=high)
{
mid=(low+high)/2;
if(ST.elem[mid]==key) return mid;
else if(ST.elem[mid]>key) high=mid-1;
else low=mid+1;
}
return 0;
}
void main()
{
int m;
SSTable ST;
Create(ST);
cout<<"please input the data that you want to search:"<<endl;
cin>>m;
cout<<"the number that you want to search is at the "<<Search(ST,m)<<endl;
cout<<"the number that you want to search is at the "<<Search_Bin(ST,m)<<endl;
}