Binary Search

Binary Search algorithm reduces the time complexity of the searching algorithm.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,fiirst,last,middle,n,search,a[10];
printf("Enter the no. of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
  scanf("%d",&array[i]);
printf("Enter the value to find");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(array[middle]<search)
{
first=middle+1;
}
else if(array[middle]==search)
{
printf("Element found at location %d",middle);
break;
}
else
{
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
  printf("Element not found");
getch();
}

kaliadevansh has written 15 articles

3 thoughts on “Binary Search

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>