Write a C program for Binary Search.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int arry[5],i,n,search,beg,end,mid;
clrscr();
printf("How many value enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Value:");
scanf("%d",&arry[i]);
}
printf("Enter value of Search...");
scanf("%d",&search);
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end)
{
if(arry[mid]<search)
beg=mid+1;
else if(arry[mid]==search)
{
printf("%d is found at %d position in list.",search,arry[mid]);
break;
}
else
end=mid-1;
mid=(beg+end)/2;
}
if(beg>end)
printf("%d is does not in list.",search);
getch();
}
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int arry[5],i,n,search,beg,end,mid;
clrscr();
printf("How many value enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Value:");
scanf("%d",&arry[i]);
}
printf("Enter value of Search...");
scanf("%d",&search);
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end)
{
if(arry[mid]<search)
beg=mid+1;
else if(arry[mid]==search)
{
printf("%d is found at %d position in list.",search,arry[mid]);
break;
}
else
end=mid-1;
mid=(beg+end)/2;
}
if(beg>end)
printf("%d is does not in list.",search);
getch();
}
Comments
Post a Comment