Binary Search Program With Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
// int list[6]={0,1,2,3,4,5};
int n,low,high,flag,mid,s;
clrscr();
printf("\n\nEnter Size:-.');
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&list[i]);
}
printf("Enter the element for search:");
scanf("%d",&s);
low=0;
high=n;
flag=1;
while(low<=high)
{
mid=(low+high)/2;
if(s<list[mid])
high=mid-1;
else if(s>list[mid])
low=mid+1;
else if(list[mid]==s)
{
printf("Search is successful...");
flag=0;
break;
}
}
if(flag)
printf("Search is unsuccessful...");
getch();
}
#include<conio.h>
void main()
{
// int list[6]={0,1,2,3,4,5};
int n,low,high,flag,mid,s;
clrscr();
printf("\n\nEnter Size:-.');
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&list[i]);
}
printf("Enter the element for search:");
scanf("%d",&s);
low=0;
high=n;
flag=1;
while(low<=high)
{
mid=(low+high)/2;
if(s<list[mid])
high=mid-1;
else if(s>list[mid])
low=mid+1;
else if(list[mid]==s)
{
printf("Search is successful...");
flag=0;
break;
}
}
if(flag)
printf("Search is unsuccessful...");
getch();
}
Binary Search Program With Source Code
Reviewed by Unknown
on
5:42:00 PM
Rating:
No comments: