Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. When using a recursive function, we don't need any loop to generate iterations. The below syntax demonstrates the working of a simple recursion function ?
def recursiveFunction():
# Statements
# ...
recursiveFunction()
# ...
recursiveFunction()
Recursive Linear Search Implementation
To perform linear search recursively, we need to define a function that calls itself with modified parameters until the element is found or the array is exhausted.
Method 1: Using Two-Pointer Approach
This approach checks elements from both ends of the array simultaneously, reducing the search space with each recursive call ?
# Recursively Linearly Search an Element in an Array
def recLinearSearch(arr, left, right, target):
if right < left:
return -1
if arr[left] == target:
return left
if arr[right] == target:
return right
return recLinearSearch(arr, left+1, right-1, target)
numbers = [1, 6, 4, 9, 2, 8]
element = 2
result = recLinearSearch(numbers, 0, len(numbers)-1, element)
if result != -1:
print('{} was found at index {}.'.format(element, result))
else:
print('{} was not found.'.format(element))
2 was found at index 4.
Method 2: Using Backward Search
This approach starts from the end of the array and moves backward until the element is found ?
# 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)
numbers = [1, 3, 6, 9, 12, 15]
element = 6
result = recLinearSearch(numbers, len(numbers)-1, element)
if result != -1:
print('{} was found at index {}.'.format(element, result))
else:
print('{} was not found.'.format(element))
6 was found at index 2.
Example: Element Not Found
When searching for an element that doesn't exist 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)
numbers = [1, 3, 6, 9, 12, 15]
element = 100
result = recLinearSearch(numbers, len(numbers)-1, element)
if result != -1:
print('{} was found at index {}.'.format(element, result))
else:
print('{} was not found.'.format(element))
100 was not found.
How Recursive Linear Search Works
The recursive linear search follows these steps:
- Base case: If the current index is invalid or the array is exhausted, return -1
- Match found: If the current element equals the target, return the index
- Recursive call: Move to the next element and repeat the search
Comparison
| Method | Search Direction | Time Complexity | Space Complexity |
|---|---|---|---|
| Two-pointer | Both ends | O(n) | O(n) |
| Backward search | End to start | O(n) | O(n) |
| Forward search | Start to end | O(n) | O(n) |
Conclusion
Recursive linear search provides an elegant way to search elements without using loops. While it has the same time complexity as iterative search, it uses additional space due to recursive function calls on the call stack.
