class binsearchprog
{
public void main()
{
int ar[]={12,23,32,34,45};
int i,lb=0,ub=4,ser;
ser=12;
int f=binsearch(ar,ser,lb,ub);
if(f == 1)
System.out.println("Ele found....");
else
System.out.println("Ele not found...");
}
int binsearch(int ar[],int ser,int lb,int ub)
{
int mb=(lb+ub)/2,i=0;
if(lb>ub)
return 0;
if(ser>ar[mb])
{
lb=mb+1;
return binsearch(ar,ser,lb,ub);
}
else if(ser<ar[mb])
{
ub=mb-1;
return binsearch(ar,ser,lb,ub);
}
else
return 1;
}
}
{
public void main()
{
int ar[]={12,23,32,34,45};
int i,lb=0,ub=4,ser;
ser=12;
int f=binsearch(ar,ser,lb,ub);
if(f == 1)
System.out.println("Ele found....");
else
System.out.println("Ele not found...");
}
int binsearch(int ar[],int ser,int lb,int ub)
{
int mb=(lb+ub)/2,i=0;
if(lb>ub)
return 0;
if(ser>ar[mb])
{
lb=mb+1;
return binsearch(ar,ser,lb,ub);
}
else if(ser<ar[mb])
{
ub=mb-1;
return binsearch(ar,ser,lb,ub);
}
else
return 1;
}
}
No comments:
Post a Comment