#include<stdio.h>
main()
{
int a[10],x,i,j,n,t;
int low,high,mid;
printf("\n Enter the number of elements=");
scanf("%d",&n);
printf("\n Enter the elements=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Your elements are=");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n Enter the elements that U search=");
scanf("%d",&x);
low=0;
high=n;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
else if(x==a[mid])
{
printf("%d is present in the %d location",x,mid+1);
break;
}
}
}
main()
{
int a[10],x,i,j,n,t;
int low,high,mid;
printf("\n Enter the number of elements=");
scanf("%d",&n);
printf("\n Enter the elements=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Your elements are=");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n Enter the elements that U search=");
scanf("%d",&x);
low=0;
high=n;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
else if(x==a[mid])
{
printf("%d is present in the %d location",x,mid+1);
break;
}
}
}
No comments:
Post a Comment