forked from dscgecbsp/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecLinSearch.c
More file actions
33 lines (33 loc) · 781 Bytes
/
recLinSearch.c
File metadata and controls
33 lines (33 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Linear Search - Recursive Approach
#include <stdio.h>
void readArr(int[],int);
int recLinSearch(int[],int,int);
void main()
{
int A[20],n,key,i,x;
printf("Enter the number of elements: ");
scanf("%d",&n);
readArr(A,n);
printf("Enter the element to search: ");
scanf("%d",&key);
x=recLinSearch(A,n,key);
if(x==-1)
printf("Element not found");
else
printf("Element found at position %d",x+1);
}
void readArr(int A[],int n)
{
int i;
printf("Enter %d number of elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
}
int recLinSearch(int A[],int n,int key)
{
if(n==0)
return -1;
else if(A[n-1]==key)
return n-1;
else return recLinSearch(A,n-1,key);
}