#include<stdio.h>
main()
{
int a[10],i,j,k,mid,beg,end,item,loc;
printf("enter the array number");
i=0;
while(i<10)
{
scanf("%d",&a[i]);
i++;
}
i=0;
while(i<10)
{
j=i+1;
while(j<10)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
j++;
}
i++;
}
printf("enter the number which you want to search");
scanf("%d",&item);
beg=0;
end=9;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=item)
{
if(item<a[mid])
{
end=mid-1;
}
else
{
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/
main()
{
int a[10],i,j,k,mid,beg,end,item,loc;
printf("enter the array number");
i=0;
while(i<10)
{
scanf("%d",&a[i]);
i++;
}
i=0;
while(i<10)
{
j=i+1;
while(j<10)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
j++;
}
i++;
}
printf("enter the number which you want to search");
scanf("%d",&item);
beg=0;
end=9;
mid=(beg+end)/2;
while(beg<=end&&a[mid]!=item)
{
if(item<a[mid])
{
end=mid-1;
}
else
{
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