forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.cpp
45 lines (45 loc) · 844 Bytes
/
binarySearch.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
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int s;
int binarySearch(int arr[],int i, int j)
{
int a=1,b=0;
int mid=(i+j)/2;
while(i<j)
{
if(arr[mid]==s)
{
return a;
}
if(s>arr[mid])
{
return binarySearch(arr,mid+1,j);
}
if(s<arr[mid])
{
return binarySearch(arr,i,mid);
}
}
return b;
}
int main()
{
int n,result;
cout<<"enter size of array: ";
cin>>n;
int arr[n];
cout<<"enter array elements: "<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
cout<<"enter element to search: ";
cin>>s;
result=binarySearch(arr,0,n-1);
if(result==1)
cout<<"element is present";
else
cout<<"element is not present";
}