Skip to content

Commit a414160

Browse files
authored
Merge pull request kelvins#315 from 007AnupamSharma/main
added Code of Linear Search in a Recursive way
2 parents 5b29b3d + e6883a9 commit a414160

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ In order to achieve greater coverage and encourage more people to contribute to
362362
</a>
363363
</td>
364364
<td> <!-- C++ -->
365-
<a href="./CONTRIBUTING.md">
366-
<img align="center" height="25" src="./logos/github.svg" />
365+
<a href="./src/cpp/LinearSearchRecursive.cpp">
366+
<img align="center" height="25" src="./logos/cplusplus.svg" />
367367
</a>
368368
</td>
369369
<td> <!-- Java -->

src/cpp/LinearSearchRecursive.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int linearSearchRecursive(int arr[], int n, int index, int target) {
6+
if (index >= n) {
7+
return -1;
8+
}
9+
10+
if (arr[index] == target) {
11+
return index;
12+
}
13+
14+
return linearSearchRecursive(arr, n, index + 1, target);
15+
}
16+
17+
int main() {
18+
int n;
19+
cout << "Enter The size of array : ";
20+
cin >> n;
21+
int arr[n];
22+
cout << "Enter the element of array : " << endl;
23+
for (int i = 0; i < n; i++) {
24+
cin >> arr[i];
25+
}
26+
27+
int target;
28+
cout << "Enter the element you need to search in the array : ";
29+
cin >> target;
30+
int answer = linearSearchRecursive(arr, n, 0, target);
31+
if (answer >= 0) {
32+
cout << "Target Element Found on index : " << answer << endl;
33+
} else {
34+
cout << "Target Element Not Found" << endl;
35+
}
36+
}

0 commit comments

Comments
 (0)