A program for binary search in C language.
Binary
Search
In
this article we will see the working of Binary Search. In Binary Search, we
need to store the elements in sequential order. This algorithm works in
partition format.
We
use three variables min, max and mid. First we need to calculate the value of
mid, by taking the average of (min+max)/2. Then we need to check whether the
search element match with the element at mid in the array. If it is then we
break the condition and further checking and declare that the element is found
at its respective position. But if not, then we need to check following two
cases.
If
the search element is greater than the element present at current position of
mid then initialize the value of min as
min=mid+1;
and start searching again
If
the search element is smaller than the element at mid , then change the value
of max and initialize it as
max=mid-1;
and start searching again. Till the element is found or till the end of
searching condition.
Try
it on rough paper by considering various values at various positions; it will
be clearer to you.
//This
program is designed to show you the working of Binary Search
#include<stdio.h>
#include<conio.h>
void
main()
{
int
i,n,src,min,max,mid=0,flag=0,a[100];
clrscr();
printf("\n Enter the size of
your Array ");
scanf("%d",&n);
printf("\n Enter the numbers
in sequential order : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the number
to be search ");
scanf("%d",&src);
min=0;max=n-1;
for(i=0;i<n;i++)
{
mid=(min+max)/2;
if(a[mid]==src)
{
flag=1;
break;
}
if(a[mid]>src)
{
max=mid-1;
}
if(a[mid]<src)
{
min=mid+1;
}
}//end of for loop
if(flag==1)
{
printf("\n Element %d is
found at the position %d ",src,mid+1);
}
else
{
printf("\n Element is not
found ");
}
getch();
}
|
Output: we are
considering the array size from user and searching the element using above
program and the output will be as follow,
Hope this article found
helpful to you,
Enjoy Programming..!!
Enjoy Programming..!!
#include
ReplyDelete#include
int main()
{
int i, f, l, m, n, search, array[10];
clrscr();
printf("\n Enter number of elements");
scanf("%d",&n);
printf("\n Enter %d integers", n);
for (i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("\n Enter value to find");
scanf("%d", &search);
f = 0;
l = n - 1;
m = (f+l)/2;
while (f <= l)
{
if (array[m] < search)
f = m + 1;
else if (array[m] == search)
{
printf("\n %d found at location %d", search, m + 1);
break;
}
else
l = m - 1;
m = (f + l)/2;
}
if (f > l)
printf("\n Not found! %d is not present in the list", search);
getch();
}
Binary Search
ReplyDelete#include
#include
int main()
{
int i, f, l, m, n, search, array[10];
clrscr();
printf("\n Enter number of elements");
scanf("%d",&n);
printf("\n Enter %d integers", n);
for (i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("\n Enter value to find");
scanf("%d", &search);
f = 0;
l = n - 1;
m = (f+l)/2;
while (f <= l)
{
if (array[m] < search)
f = m + 1;
else if (array[m] == search)
{
printf("\n %d found at location %d", search, m + 1);
break;
}
else
l = m - 1;
m = (f + l)/2;
}
if (f > l)
printf("\n Not found! %d is not present in the list", search);
getch();
}
//This program is designed to show you the working of Binary Search
ReplyDelete#include
void main()
{
int i,n,src,min,max,mid=0,flag=0,a[100];
printf("\n Enter the size of your Array ");
scanf("%d",&n);
printf("\n Enter the numbers in sequential order : ");
for(i=0;isrc)
{
max=mid-1;
}
if(a[mid]<src)
{
min=mid+1;
}
}//end of for loop
if(flag==1)
{
printf("\n Element %d is found at the position %d ",src,mid+1);
}
else
{
printf("\n Element is not found ");
}
}
//This program is designed to show you the working of Binary Search
ReplyDelete#include
void main()
{
int i,n,src,min,max,mid=0,flag=0,a[100];
printf("\n Enter the size of your Array ");
scanf("%d",&n);
printf("\n Enter the numbers in sequential order : ");
for(i=0;isrc)
{
max=mid-1;
}
if(a[mid]<src)
{
min=mid+1;
}
}//end of for loop
if(flag==1)
{
printf("\n Element %d is found at the position %d ",src,mid+1);
}
else
{
printf("\n Element is not found ");
}
}
/*Binary search*/
ReplyDelete#include
void main()
{
int i,n,src,min,max,mid=0,flag=0,a[100];
printf("\n Enter the size of your Array ");
scanf("%d",&n);
printf("\n Enter the numbers in sequential order : ");
for(i=0;isrc)
{
max=mid-1;
}
if(a[mid]<src)
{
min=mid+1;
}
}//end of for loop
if(flag==1)
{
printf("\n Element %d is found at the position %d ",src,mid+1);
}
else
{
printf("\n Element is not found ");
}
}
/* Binary Search*/
ReplyDelete#include
void main()
{
int i,n,src,min,max,mid=0,flag=0,a[100];
printf("\n Enter the size of your Array ");
scanf("%d",&n);
printf("\n Enter the numbers in sequential order : ");
for(i=0;isrc)
{
max=mid-1;
}
if(a[mid]<src)
{
min=mid+1;
}
}//end of for loop
if(flag==1)
{
printf("\n Element %d is found at the position %d ",src,mid+1);
}
else
{
printf("\n Element is not found ");
}
}
Binary Search
ReplyDelete#include
#include
int main()
{
int i, f, l, m, n, search, array[10];
clrscr();
printf("\n Enter number of elements");
scanf("%d",&n);
printf("\n Enter %d integers", n);
for (i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("\n Enter value to find");
scanf("%d", &search);
f = 0;
l = n - 1;
m = (f+l)/2;
while (f <= l)
{
if (array[m] < search)
f = m + 1;
else if (array[m] == search)
{
printf("\n %d found at location %d", search, m + 1);
break;
}
else
l = m - 1;
m = (f + l)/2;
}
if (f > l)
printf("\n Not found! %d is not present in the list", search);
getch();
}
/**************************************************************
ReplyDeletebinary search
**************************************************************/
#include
int main()
{
int c,first,last,middle,n,search,array[100];
printf("\nEnter number of elements:");
scanf("%d",&n);
printf("\nEnter %d integer(s)",n);
for(c=0;clast)
printf("\nNot found!%d is not present",search);
return 0;
}
/**************************************************************
ReplyDeletebinary search
**************************************************************/
#include
int main()
{
int c,first,last,middle,n,search,array[100];
printf("\nEnter number of elements:");
scanf("%d",&n);
printf("\nEnter %d integer(s)",n);
for(c=0;clast)
printf("\nNot found!%d is not present",search);
return 0;
}