Skip to content

Commit 9c8aea4

Browse files
Create binarySearch.cpp
1 parent bd7e785 commit 9c8aea4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

binarySearch.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int binarySearch(vector<int> arr, int l, int r, int x)
6+
{
7+
if (r >= l) {
8+
int mid = l + (r - l) / 2;
9+
10+
if (arr[mid] == x)
11+
return mid;
12+
13+
if (arr[mid] > x)
14+
return binarySearch(arr, l, mid - 1, x);
15+
16+
return binarySearch(arr, mid + 1, r, x);
17+
}
18+
return -1;
19+
}
20+
21+
22+
int main() {
23+
vector<int> arr = {-10, -7, 0, 3, 4, 9};
24+
cout << binarySearch(arr, 0, 5, 0) << endl;
25+
26+
}

0 commit comments

Comments
 (0)