#include <iostream>
const int MAXSIZE=100;
typedef struct
{
int elem[MAXSIZE];
int length;
} S_TBL;
class Search
{
public:
//有序表的二分查找实现
int Binary_Search(S_TBL tb1,int kx)
{
int mid,low,high;
int flag=0;
low=1;
high=tb1.length;
while(low<=high)
{
mid=(low+high)/2;
if(kx<tb1.elem[mid]) high=mid-1;
else if(kx>tb1.elem[mid]) low=mid+1;
else { flag=mid;break;}
}
return flag;
}
};
int main()
{
S_TBL sb1;
sb1.elem[0]=35;
sb1.elem[1]=14;
sb1.elem[2]=18;
sb1.elem[3]=21;
sb1.elem[4]=23;
sb1.elem[5]=29;
sb1.elem[6]=31;
sb1.elem[7]=35;
sb1.elem[8]=38;
sb1.elem[9]=42;
sb1.elem[10]=46;
sb1.elem[11]=49;
sb1.elem[12]=52;
sb1.length=12;
Search se;
cout<<"search :"<<se.Binary_Search(sb1,35)<<endl;
cin.get();
return 0;
}