Recursive linear search java – Java Program to Implement Linear Search by Using Recursion

Recursive linear search java: In the previous article, we have discussed about Java Program to Check Perfect Number by Using Recursion

In this program we are going to see how to implement linear search by using recursion by Java programming language.

Java Program to Implement Linear Search by Using Recursion

Lets start with an example.

Assume there is an array say A which has 5 elements {77, 82, 100, 17, 95}
We need to check if the specified element is present or not.
Example 99: Not present, 100: present

Now let’s see different ways to implement linear search by using Recursion.

Method-1: Java Program to Implement Linear Search By Using Static Input and Recursion

Approach:

  • Declare an integer Array ‘arr’ and initialize it elements.
  • Declare and initiate an integer variable say ‘value’ as 94
  • Call a user defined method search() and pass the array ‘arr[]’ with first index ‘0’,  last index ‘arr.length-1’ and the specific element to be searched ‘value’ as parameter.
  • Inside the user defined method, check if the last index is less than the first index then return -1 else if the first index is equal to value then return first index else call search() method recursively to search until that specific value is found inside the array.
  • Now, the result of user defined method search() is stored in an integer variable say ‘index’.
  • Print the result.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    // defined main() method where the program starts executing
    public static void main(String[] args)
    {
        //declare and initialize an integer variable array arr[]
        int[] arr = {77, 82, 100, 17, 95};
        //declare and initialize an integer variable ‘value’ to search that specific number inside the array
        int value = 94; 
        //calling the user defined method search()
        //and store the result value inside an integer variable say ‘index’  
        int index = search(arr, 0, arr.length-1, value);
        // check if the value is present in the array then print the result
        if (index != -1)
           System.out.println("The element " + value + " is present at index " + index);
        else
            System.out.println("The element " + value + " is not present in the array");
    }

    // defined search() method with arr[], first index, last index and value as parameter to search that specific value inside the array
    public static int search(int arr[], int firstIndex, int lastIndex, int value) 
    {
        // if the last index is less than the first index then return -1
        if (lastIndex < firstIndex)
           return -1;
        // if the first index is equal to value then return first index
        if (arr[firstIndex] == value)
           return firstIndex;
        // else call search() method recursively to search until that specific value is found inside the array
        return search(arr, firstIndex+1, lastIndex, value);
    }
}
Output:

The element 94 is not present in the array

Method-2: Java Program to Implement Linear Search By Using User Input and Recursion

Approach:

  • Create a scanner class.
  • Declare an integer variable say ‘n
  • Prompt the user to enter the size of the array as value of n.
  • Declare an integer Array ‘arr[]’ .
  • Prompt the user to enter the elements inside the array.
  • Declare and initiate an integer variable say ‘value
  • Prompt the user to enter the value of value
  • Call a user defined method search() and pass the array ‘arr[]’ with first index ‘0’,  last index ‘arr.length-1’ and the specific element to be searched ‘value’ as parameter.
  • Inside the user defined method, check if the last index is less than the first index then return -1 else if the first index is equal to value then return first index else call search() method recursively to search until that specific value is found inside the array.
  • Now, the result of user defined method search() is stored in an integer variable say ‘index’.
  • Print the result.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter no. of elements you want in array:");
        //prompting the user to enter a integer value
        int n = s.nextInt();
        // declare the array and initialize it with user input
        int arr[] = new int[n];
        //get all the elements sequentially inside the array
        System.out.println("Enter all the elements in the array:");
        for(int i = 0; i < n; i++)
            arr[i] = s.nextInt();
        // declare the specific value to be search inside the array
        System.out.println("Enter the specific element you want to search in the array:");
        int value = s.nextInt();
        //calling the user defined method search()
        //and store the result value inside an integer variable say ‘index’  
        int index = search(arr, 0, arr.length-1, value);
        // check if the value is present in the array then print the result
        if (index != -1)
           System.out.println("The element " + value + " is present at index " + index);
        else
            System.out.println("The element " + value + " is not present in the array");
    }

    // defined search() method with arr[], first index, last index and value as parameter to search that specific value inside the array
    public static int search(int arr[], int firstIndex, int lastIndex, int value) 
    {
        // if the last index is less than the first index then return -1
        if (lastIndex < firstIndex)
           return -1;
        // if the first index is equal to value then return first index
        if (arr[firstIndex] == value)
           return firstIndex;
        // else call search() method recursively to search until that specific value is found inside the array
        return search(arr, firstIndex+1, lastIndex, value);
    }
}
Output:

Enter no. of elements you want in array:
7
Enter all the elements in the array:
1
17
78
86
94
22
175
Enter the specific element you want to search in the array:
94
The element 94 is present at index 4

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: