#include<stdio.h>
#include<conio.h>
int BSearch(int[],int,int);
main()
{
int search=0,n=0,i=0,j=0,t=0,a[100],result=0;
clrscr();
printf("\n Enter How Many Numbers :");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("\n Enter Search Element :");
scanf("%d",&search);
result=BSearch(a,n,search);
if(result==-1)
printf("\n Element Not Found");
else
printf("%d Found At %d Position",search,result);
}
int BSearch(int a[],int n,int s)
{
int low=0,high=n-1,mid=0;
while(low<high)
{
mid=(low+high)/2;
if(a[mid]==s)
{
return mid;
}
else
{
if(a[mid]<s)
low=mid+1;
else
high=mid-1;
}
}
return -1;
}
#include<conio.h>
int BSearch(int[],int,int);
main()
{
int search=0,n=0,i=0,j=0,t=0,a[100],result=0;
clrscr();
printf("\n Enter How Many Numbers :");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("\n Enter Search Element :");
scanf("%d",&search);
result=BSearch(a,n,search);
if(result==-1)
printf("\n Element Not Found");
else
printf("%d Found At %d Position",search,result);
}
int BSearch(int a[],int n,int s)
{
int low=0,high=n-1,mid=0;
while(low<high)
{
mid=(low+high)/2;
if(a[mid]==s)
{
return mid;
}
else
{
if(a[mid]<s)
low=mid+1;
else
high=mid-1;
}
}
return -1;
}
No comments:
Post a Comment