C Program to serach an element using binary search...*/
#include
#include
#include
int num[20], max, e, small, big, v, m,n,ans;
void receve()
{
printf("Enter the elements of the array\n");
for(m=0;m
printf("Enter the elements to be searched: ");
scanf("%d",&e);
}
void show()
{
printf("The sorted array is\n");
for(m=0;m
printf("\n");
ans=bs(0,max-1);
if(ans==(-1))
printf("Element not found\n");
else
printf("Element found at position: %d",ans+1);
}
void bsort()
{
for(m=0;m
{
v=num[n];
num[n]=num[n+1];
num[n+1]=v;
}
}
int bs(int small, int big)
{
int mid=(small+big)/2;
if(small>big)
return -1;
else if(e>num[mid])
return bs(mid+1,big);
else if(e
else
return mid;
}
main()
{
clock_t start, end;
printf("Enter the size of the array\n");
scanf("%d",&max);
receve();
start=clock();
bsort();
end=clock();
show();
printf("The time was: %f",(end-start)/CLK_TCK);
getche();
return;
}
No comments:
Post a Comment