forked from ddhira123/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSEARCH.CPP
33 lines (33 loc) · 854 Bytes
/
BSEARCH.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<conio.h>
int Bsearch(int[ ],int,int);
void main()
{ clrscr();
int AR[50],item,n,index;
cout<<"Enter desired array size(max. 50):) ";
cin>>n;
cout<<"Enter array elements:\n";
for(int i=0;i<n;i++)
{ cin>>AR[i]; }
cout<<"\nEnter element to be searched for: ";
cin>>item;
index=Bsearch(AR,n,item);
if(index==-1)
cout<<"\nSorry!!Given element could not be found.\n";
else
cout<<"\nElement found at index:"<<index<<",Position:"<<index+1<<endl;
getch();
}
int Bsearch(int AR[ ],int size,int item)
{ int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==AR[mid]) { return mid; }
else if(item>AR[mid])
beg=mid+1;
else
last=mid-1;
} return -1;
}