Linear search Program using recursion

Problem statement:- Program to Implement Linear search using recursion.

Data requirement:-

   Input Data:-n, arr

  Output Data:-
String Output

Program in C

Here is the source code of the C Program to Implement Linear search using recursion.

Code:

#include<stdio.h>
int Linear_search(int arr[], int Search_ele, int n)
{
    int i;
    static int temp=0;
    if(n>0)
    {
        i=n-1;
          if(arr[i]==Search_ele)
            temp=1;
        Linear_search(arr,Search_ele,i);
    }
return temp;
}
int main()
{
    int n,j;
    printf("Enter your array size:");
    scanf("%d",&n);
    int arr[n];
    printf("Enter the Array Element:");
    for(j=0;j<n;j++)
    {
        scanf("%d",&arr[j]);
    }
    int Search_ele;
    printf("Enter the search element:");
    scanf("%d",&Search_ele);
    if(Linear_search(arr,Search_ele,n)==1)
        printf("Element found....");
    else
        printf("Element not found....");
    return 0;
}

Input/Output:
Enter your array size:5
Enter the Array Element:4 8 10 3 5
Enter the search element:10
Element found....

Program in C++

Here is the source code of the C++ Program to Implement Linear search using recursion.

Code:

#include<iostream>
using namespace std;
int Linear_search(int arr[], int Search_ele, int n)
{
    int i;
    static int temp=0;
    if(n>0)
    {
        i=n-1;
          if(arr[i]==Search_ele)
            temp=1;
        Linear_search(arr,Search_ele,i);
    }
return temp;
}
int main()
{
    int n,j;
    cout<<"Enter your array size:";
    cin>>n;
    int arr[n];
    cout<<"Enter the Array Element:";
    for(j=0;j<n;j++)
    {
        cin>>arr[j];
    }
    int Search_ele;
    cout<<"Enter the search element:";
    cin>>Search_ele;
    if(Linear_search(arr,Search_ele,n)==1)
       cout<<"Element found....";
    else
        cout<<"Element not found....";
    return 0;
}

Input/Output:
Enter your array size:6
Enter the Array Element:9 13 55 65 100 2
Enter the search element:5
Element not found....

Program in Java

Here is the source code of the Java Program to Implement Linear search using recursion.

Code:

import java.util.Scanner;
public class LinearSearchRecursion {
int temp=0;
int Linear_search(int arr[], int Search_ele, int n)
{
    int i;
    if(n>0)
    {
        i=n-1;
          if(arr[i]==Search_ele)
            temp=1;
        Linear_search(arr,Search_ele,i);
    }
return temp;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i;
System.out.println("Enter your Array Size:");
n=cs.nextInt();
int arr[]=new int[n];
System.out.println("Enter the Array Element:");
for(i=0;i<n;i++)
{
arr[i]=cs.nextInt();
}
System.out.println("Enter the search element:");
int Search_ele=cs.nextInt();
LinearSearchRecursion ob=new LinearSearchRecursion();
if(ob.Linear_search(arr,Search_ele,n)==1)
System.out.println("Element found....");
else
    System.out.println("Element not found....");
cs.close();
}
}

Input/Output:
Enter your Array Size:
8
Enter the Array Element:
4 5 10 13 52 33 3 7
Enter the search element:
10
Element found....

Program in Python

Here is the source code of the Python program to Implement Linear search using recursion.

Code:

temp=0
def Linear_search(arr,Search_ele,n):
    global temp
    if(n>0):
        i=n-1
        if(arr[i]==Search_ele):
            temp=1
        Linear_search(arr, Search_ele, i)
    return temp
arr=[]
n = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,n):
    num = int(input())
    arr.append(num)
Search_ele=int(input("Enter the search element:"))
if(Linear_search(arr,Search_ele,n)==1):
    print("Element found....")
else:
    print("Element not found....")

Input/Output:

Post a Comment

0 Comments