Skip to content

Commit 2c06dd1

Browse files
committed
Create Linear search(Recursively).cpp
1 parent 5b91ba8 commit 2c06dd1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Linear search(Recursively).cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<iostream>
2+
using namespace std;
3+
int linearSearch(int *a,int i,int n,int key)
4+
{
5+
// If element is not present then it will return -1
6+
if(i==n)
7+
{
8+
return -1;
9+
}
10+
//If the element is present then it will return the index where the element is present
11+
if(a[i]==key)
12+
{
13+
return i;
14+
}
15+
return linearSearch(a,i+1,n,key);
16+
}
17+
int main()
18+
{
19+
int a[6]={1,3,2,5,6,9};
20+
int n=sizeof(a)/sizeof(int);
21+
int key = 15;
22+
cout<<linearSearch(a,0,n,key);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)