精彩编程6


#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;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值