Python Program to Recursively Linearly Search an Element in an Array


Linear search is the simplest method of searching for an element in an array. It is a sequential searching algorithm that starts from one end and checks every element of the array until the desired element is found.

Recursion means a function that calls itself, while using a recusive function we need to use any loop to generate the iterations. The below syntax demonistrates the working of a simple recursion function.

def rerecursiveFun():
   Statements
   ...   
   rerecursiveFun()
   ...
rerecursiveFun

Linear Search of an Element Recursively

Linearly search an element recursively from an array, is only possible by using the functions. To define a function we need to use the def keyword in python.

In this article, we will learn how to Recursively Linearly Search an element in an Array in python. Here we will use a Python list in place of an array because python doesn’t have a specific data type to represent arrays.

Example

We will recursively call the function recLinearSearch() by decrementing the size of the array. If the size array becomes less than zero it means the element is not present in the array and we returns -1. If a match is found, then it will return the size in which index the element was found.

# Recursively Linearly Search an Element in an Array  
def recLinearSearch( arr, l, r, x): 
   if r < l: 
      return -1
   if arr[l] == x: 
      return l 
   if arr[r] == x: 
      return r 
   return recLinearSearch(arr, l+1, r-1, x) 
     
lst = [1, 6, 4, 9, 2, 8]
element = 2
res = recLinearSearch(lst, 0, len(lst)-1, element) 
  
if res != -1:
   print('{} was found at index {}.'.format(element, res))
else:
   print('{} was not found.'.format(element))

Output

2 was found at index 4.

Example

Let’s take another example to search an element in an array.

# Recursively Linearly Search an Element in an Array  
def recLinearSearch(arr, curr_index, key):
   if curr_index == -1:
      return -1
   if arr[curr_index] == key:
      return curr_index
   return recLinearSearch(arr, curr_index-1, key)
arr = [1, 3, 6, 9, 12, 15]
element = 6
res = recLinearSearch(arr, len(arr)-1, element) 
  
if res != -1:
   print('{} was found at index {}.'.format(element, res))
else:
   print('{} was not found.'.format(element))

Output

6 was found at index 2.

Example

Take another example to search for an element 100 in the array.

# Recursively Linearly Search an Element in an Array  
def recLinearSearch(arr, curr_index, key):
   if curr_index == -1:
      return -1
   if arr[curr_index] == key:
      return curr_index
   return recLinearSearch(arr, curr_index-1, key)     
arr = [1, 3, 6, 9, 12, 15]
element = 100
res = recLinearSearch(arr, len(arr)-1, element) 
  
if res != -1:
   print('{} was found at index {}.'.format(element, res))
else:
   print('{} was not found.'.format(element))

Output

100 was not found.

In the above example, element 100 is not found in the given array.

These are examples of Recursively Linearly Search an element in an Array using python programming.

Updated on: 15-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements