
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Recursively Linearly Search an Element in an Array
In this article, we will understand how to recursively linearly search an element in an array. Linear search is a very simple search algorithm wherein a sequential search is done for all items one by one.
Below is a demonstration of the same −
Suppose our input is −
Input array: 14 20 35 47 50 65 72 81 90 99 Key element: 72
The desired output would be −
The element 72 is present at position: 6
Algorithm
Step 1 - START Step 2 - Declare a string array namely input_array, two integer namely key_element and index Step 3 - Define the values. Step 4 - Iterate through the array. Step 5 - Define the element to be searched. Invoke the recursive method by passing these parameters. Step 6 - Define an ‘if’ condition with the condition that a failure would return -1, otherwise the position of the element itself. Step 7 - Display this on the console. Step 8 - Stop
Example 1
Here, we show linear search on integers.
public class LinearSearch { static int recSearch(int input_array[], int l, int r, int key_element) { if (r < l) return -1; if (input_array[l] == key_element) return l; if (input_array[r] == key_element) return r; return recSearch(input_array, l+1, r-1, key_element); } public static void main(String[] args) { int input_array[] = {14, 20, 35, 47, 50, 65, 72, 81, 90, 99}; System.out.println("The elements of the array is defined as "); for (int i : input_array) { System.out.print(i +" "); } int key_element = 72; System.out.println("\n\nThe elements to be searched in the array is: " + key_element); int index = recSearch(input_array, 0, input_array.length-1, key_element); if (index != -1) System.out.println("\nThe element " + key_element + " is present at position: " + index); else System.out.println("Element " + key_element + " is not present: "); } }
Output
The elements of the array is defined as 14 20 35 47 50 65 72 81 90 99 The elements to be searched in the array is: 72 The element 72 is present at position: 6
Example 2
Here, we show linear search on strings.
public class Demo { static int recSearch(String input_array[], int l, int r, String key_element) { if (r < l) return -1; if (input_array[l] == key_element) return l; if (input_array[r] == key_element) return r; return recSearch(input_array, l+1, r-1, key_element); } public static void main(String[] args) { String input_array[] = { "Scala", "Java", "Python", "Mysql"}; System.out.println("The elements of the array is defined as "); for (String i : input_array) { System.out.print(i +" "); } String key_element = "Java"; System.out.println("\n\nThe elements to be searched in the array is: " + key_element); int index = recSearch(input_array, 0, input_array.length-1, key_element); if (index != -1) System.out.println("\nThe element " + key_element + " is present at position: " + index); else System.out.println("Element " + key_element + " is not present: "); } }
Output
The elements of the array is defined as Scala Java Python Mysql The elements to be searched in the array is: Java The element Java is present at position: 1
- Related Articles
- Recursive program to linearly search an element in a given array in C++
- Recursive program to find an element in an array linearly.
- Swift Program to Search an Element in an Array
- Write a Golang program to search an element in an array
- C program to search an array element using Pointers.
- C++ program to search an element in a sorted rotated array
- Java Program to implement Binary Search on an array
- C++ Program to Find Minimum Element in an Array using Linear Search
- C++ Program to Find Maximum Element in an Array using Binary Search
- Write a Golang program to search an element in a sorted array
- How to search for an element in JavaScript array?
- How to search for an array element in TypeScript?
- Search an element of ArrayList in Java
- Search an element of Vector in Java
- Golang program to search an element in the slice

Advertisements