#include<iostream>
usingnamespace std;
int Bsearch(int[],int,int);
int main()
{
int ar[50] , index , N , ITEM ,i;
cout << "\nEnter The Number Of ELEMENTS YOU WANT IN THE ARRAY : ";
cin >> N;
cout << "\nEnter The Elements IN The ARRAY : ";
for (i = 0 ; i < N ; ++i)
{
cout << endl;
cin >> ar[i];
}
cout << "\nEnter The Element to Be Searched For :";
cin >> ITEM;
index = Bsearch(ar,N,ITEM);
if(index == -1)
cout << "\nSORRY ELEMENT NOT FOUND!";
else
cout << "INDEX : " << index << " Position : " << index +1 ;
return 0 ;
}
int Bsearch(int ar[],int size , int item)
{
int beg,last,mid;
beg = 0 ; last = mid -1 ;
while(beg<=last)
{
mid = (beg+last) / 2;
if (item == ar[mid]) return mid;
elseif (item > ar[mid])
beg = mid + 1 ;
else
last = mid - 1;
}
return -1;
}