Skip to content

Commit d798aee

Browse files
added linear Search code in a recursive way
1 parent 6c29286 commit d798aee

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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+
#include <vector>
3+
using namespace std;
4+
5+
bool linearSearchRecursive(int arr[], int n, int index, int target){
6+
if(index >= n){
7+
return false;
8+
}
9+
10+
if(arr[index] == target){
11+
return true;
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+
bool answer = linearSearchRecursive(arr, n, 0, target);
31+
if(answer){
32+
cout<<"Found"<<endl;
33+
}else{
34+
cout<<"Not Found"<<endl;
35+
}
36+
}

src/cpp/LinearSearchRecursive.exe

44.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)