What is binary search?
akash3343:
hi
Answers
Answered by
3
binary search algorithm is used to find the position of a specific value contained in a sorted array. Working with the principle of divide and conquer, this search algorithm can be quite fast, but the caveat is that the data has to be in a sorted form. It works by starting the search in the middle of the array and working going down the first lower or upper half of the sequence.
Answered by
0
Binary search is used to find the element in sorted array in any order(ascending or descending).
BINARY SEARCH PROGRAM:-
#include<iostream.h>
#include<conio.h>
void BSearch(int a[],int size,int item);
void main ()
{
clrscr();
int arr[5]={4,8,2,1,5,7};
int size= sizeof(arr[5])/2;
int item;
cout<<"enter item";
cin>>item;
BSearch(arr[],size,item);
getch();
}
void BSearch(int a[],int size, int item)
{
int beg=0,end=size,mid,POS=0;
while (beg<end)
{
mid = (beg+end)/2;
if(mid==item)
{
cout<<"position="<<POS+1;
POS=1;
break;
}
else if(item>a[mid])
{
beg=mid +1;
}
else
{
end=mid-1;
}
}
if(POS==0)
{
cout<<not found;
}
}
Similar questions