This program runs linear search recursively in an array using recursion in c++ code
How Program Works :
Program takes size of array
Input elements in array
Passing array, key and size to the recursive function recursiveLinearSearch(int array[],int key, int size)
Recursive function calls it self until certain conditions fulfill
Function returns 1 if record found in array else returns -1
C++ Program
#include<iostream>
using namespace std;
int recursiveLinearSearch(int array[],int key,int size) {
size=size-1;
if(size <0) {
return -1;
} else if(array[size]==key) {
return 1;
} else {
return recursiveLinearSearch(array,key,size);
}
}
int main() {
cout<<"Enter The Size Of Array: ";
int size;
cin>>size;
int array[size], key,i;
// Taking Input In Array
for (int j=0;j<size;j++) {
cout<<"Enter "<<j<<" Element : ";
cin>>array[j];
}
//Your Entered Array Is
for (int a=0;a<size;a++) {
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}
cout<<"Enter Key To Search in Array";
cin>>key;
int result;
result=recursiveLinearSearch(array,key,size--);
if(result==1) {
cout<<"Key Found in Array ";
} else {
cout<<"Key NOT Found in Array ";
}
return 0;
}