Binary Search:- Binary Search is a searching technique which is used to locate items in ordered list of particular number.T his technique i...
Binary Search:-Binary Search is a searching technique which is used to locate items in ordered list of particular number.This technique is used in c,c++,java,c# etc.
Complexity of Binary Search is o(logn).
Binary Search Example and Algorithm:-
Complexity of Binary Search is o(logn).
Binary Search Example and Algorithm:-
#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,x,n,a,l,f,flag;
cout<<"Enter Size";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
flag=0;
cout<<"Enter Element to be Search";
cin>>x;
l=n-1;
f=0;
while(f<=l)
{
m=(f+l)/2;
if(a[m]==x)
{
flag=1;
break;
}
if(x>a[m])
{
f=m+1;
else
l=m-1;
}
if(flag==1)
cout<<"Element Found"<<m;
else
cout<<"NOT FOUND";
getch();
}
If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.
COMMENTS