#include
main()
{
int ar[15],i,n,ele,pos;
int search(int[],int);
for(i=0;i<=14;i++)
{
printf("Enter element");
scanf("%d",&ar[i]);
}
printf("\n Enter element to be searched");
scanf("%d",&ele);
pos=search(ar,ele);
printf("position=%d",pos);
}
int search(int ar[],int ele)
{
int beg,end,mid,pos;
pos=0;
beg=0;
end=14;
while(beg<=end)
{
mid=(beg+end)/2;
if(ar[mid]==ele)
{
pos=mid+1;
return(pos);
}
else
if(ele>ar[mid])
beg=mid+1;
else
end=mid-1;
}
}
Output:
Enter element401
Enter element402
Enter element403
Enter element404
Enter element405
Enter element406
Enter element407
Enter element408
Enter element409
Enter element410
Enter element411
Enter element412
Enter element413
Enter element414
Enter element415
Enter element to be searched413
position=13
No comments:
Post a Comment