#include<stdio.h>
main()
{
int a[10],i,j,k,mid,beg,end,item,loc;
printf("enter the array number");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
printf("enter the number which you want to search");
scanf("%d",&item);
beg=0;
end=9;
mid=(beg+end)/2;
for(;beg<=end&&a[mid]!=item;)
{
if(item<a[mid])
{
end=mid-1;
}
else
{ 0
beg=mid+1;
}
mid=(beg+end)/2;
}
if(a[mid]==item)
loc=mid;
else
loc=-2;
if(loc>=0)
printf("%d position is %d",item,loc);
else
printf("%d not found",item);
return 0;
}
main()
{
int a[10],i,j,k,mid,beg,end,item,loc;
printf("enter the array number");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
printf("enter the number which you want to search");
scanf("%d",&item);
beg=0;
end=9;
mid=(beg+end)/2;
for(;beg<=end&&a[mid]!=item;)
{
if(item<a[mid])
{
end=mid-1;
}
else
{ 0
beg=mid+1;
}
mid=(beg+end)/2;
}
if(a[mid]==item)
loc=mid;
else
loc=-2;
if(loc>=0)
printf("%d position is %d",item,loc);
else
printf("%d not found",item);
return 0;
}
for more codes you can Visit http://codesofprogramming.blogspot.in/
No comments:
Post a Comment