-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbinary.cpp
More file actions
49 lines (40 loc) · 777 Bytes
/
binary.cpp
File metadata and controls
49 lines (40 loc) · 777 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
47
48
49
#include<bits/stdc++.h>
using namespace std;
int binarysearch(int arr[],int beg,int end,int info)
{
int mid=(beg+end)/2;
do
{
mid=(beg+end)/2;
if(info<arr[mid])
{
end=mid;
}
else if(info>arr[mid])
{
beg=mid+1;
}
else if(arr[mid]==info)
{
return mid;
}
}while(beg<=end);
cout<<"value not found"<<endl;
return -1;
}
int main()
{
int size,x;
cout<<"Enter size of array"<<endl;
cin>>size;
int arr[size];
cout<<"Enter elements of array"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
cout<<"Enter item to search"<<endl;
cin>>x;
sort(arr,arr+size);
int y=binarysearch(arr,0,size-1,x);
if(y!=-1)
cout<<"value found"<<endl;
}