-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbinary.cpp
More file actions
46 lines (43 loc) · 855 Bytes
/
Copy pathbinary.cpp
File metadata and controls
46 lines (43 loc) · 855 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,l,h,mid,n;
cout<<"Enter no. of elements you want to insert :-"<<endl;
cin>>n;
int arr[n];
cout<<"Enter nos. in increasing order"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Nos. are:-"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
}
cout<<endl<<"Enter element you want to search"<<endl;
cin>>a;
l=0;
h=n;
int key=-1;
while(l<=h)
{
mid=(l+h)/2;
if(arr[mid]==a)
{
cout<<"No. found at"<<mid;
key++;
break;
}
else
if(arr[mid]>a){
h=mid-1;
}
else
if(arr[mid]<a){
l=mid+1;
}
}
if(key==-1){
cout<<"Not Present";}
}