forked from Girl-Code-It/Beginner-CPP-Submissions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
34 lines (34 loc) · 785 Bytes
/
binarySearch.cpp
File metadata and controls
34 lines (34 loc) · 785 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
#include <iostream>
using namespace std;
int binarySearch(int arr[], int num ,int low ,int high){
int mid,ans = 0;
if(low<=high){
mid=(low+high)/2;
if(arr[mid]==num){
ans = 1;
}
else if(arr[mid]<num){
return binarySearch(arr,num,mid+1,high);
}
else{
return binarySearch(arr,num,low,mid-1);
}
}
return ans;
}
int main() {
int n,num,ans;
cin>>n>>num;
int arr[n+1];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int low=0,high=n-1;
ans = binarySearch(arr,num,low,high);
if(ans==0){
cout<<"The search number is not found in the array.";
}
else{
cout<<"The search number found in the array";
}
}