-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search_using_recursion.cpp
More file actions
72 lines (47 loc) · 1.03 KB
/
binary_search_using_recursion.cpp
File metadata and controls
72 lines (47 loc) · 1.03 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Data-Structures-And-Algorithms
Here I will post my regular DSA problems
//Binary search using recursion
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
class bin{
public:
int a[100],n,l,r,mid,x;
// Getting the input sorted array
void input(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
l=0;
r=n-1;
x=8;
int result=bin_search(a,l,r,x);
cout<<result;
}
// Recursive function for binary search
int bin_search (int a[],int l,int r,int x) {
int mid;
if(l>r){
cout<<"Not found";
}
else{
mid=(l+r)/2;
if(a[mid]==x){
return mid;
}
else if(a[mid]>x){
return bin_search(a,l,mid-1,x);
}
else{
return bin_search(a,l+1,r,x);
}
}
}
};
int main(){
bin b;
b.input();
return 0;
}