Skip to content

Commit 2803f0a

Browse files
authored
Add files via upload
1 parent eed9450 commit 2803f0a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Recursive Binary Search.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<iostream>
2+
using namespace std;
3+
int array[15],i,x,n;
4+
int BinarySearch(int low , int high)
5+
{
6+
if (low >high)
7+
{
8+
return -1;
9+
}
10+
else
11+
{
12+
int mid = (low+high)/2;
13+
if (array[mid]==x)
14+
{
15+
return mid;
16+
}
17+
else if (array[mid]>x)
18+
{
19+
return BinarySearch(low, n-1);
20+
}
21+
else
22+
{
23+
return BinarySearch(mid+1,high);
24+
}
25+
}
26+
}
27+
int main ()
28+
{
29+
cout<<"Input the size of the element: " ;
30+
cin>> n;
31+
cout<<"Input the value of the array : ";
32+
for (i=0; i<n;i++)
33+
{
34+
cin>>array[i];
35+
}
36+
cout<<"Input the value you want to search : ";
37+
cin>>x;
38+
int result = BinarySearch(0, n - 1);
39+
if (result == -1) {
40+
cout << "The value is not found in the array.";
41+
} else {
42+
cout << "The value is found at index: " << result;
43+
}
44+
return 0;
45+
}

0 commit comments

Comments
 (0)