#include"stdio.h"
#include"conio.h"
void bsearch(int a[],int n,int item)
{
int beg=0,last=n-1,mid;
while(beg<=last){
mid=(beg+last)/2;
if(a[mid]==item){
printf("the element is present in the index %d",mid);
break; }
else if(a[mid]>last)
last=mid-1;
else
beg=mid+1;
}
if(beg>last)
printf("element is not present");
}
void main(){
clrscr();
int a[20],n,item,i;
printf("enter the no of elements you want to enter");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i< n;i++){
scanf("%d",&a[i]);
}
printf("enter the element you want to search");
scanf("%d",&item);
bsearch(a,n,item);
}
Monday, September 1, 2008
Binary search
Posted by Nanya at 11:28 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment