


Python program to recursively linearly search elements in an array
Linear search is the simplest way to search for elements in an array. It is a sequential search algorithm that starts from one end and checks each element of the array until the required element is found.
Recursion refers to a function calling itself. When using a recursive function, we need to use any loop to generate the iteration. The syntax below demonstrates how a simple recursive function works.
def rerecursiveFun(): Statements ... rerecursiveFun() ... rerecursiveFun
Linear search for elements recursively
Linear search for an element recursively from an array can only be achieved by using functions. In Python, to define a function, we need to use the def keyword.
In this article, we will learn how to linearly search elements in an array recursively in Python. Here, we will use Python lists instead of arrays since Python does not 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 of the array becomes negative, meaning the element is not in the array, we return -1. If a match is found, the index position where the element is located is returned.
# 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 look at another example of searching for elements 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 searching for element 100 in the array as another example.
# 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 using Python programming to recursively linearly search elements in an array.
The above is the detailed content of Python program to recursively linearly search elements in an array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method. Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements. Let’s look at various input and output scenarios for this −Input −Arr={12,67,99,76,32}; Output −Maximum value in the array: 99 Explanation &mi

Use the `Arrays.stream()` function in Java to convert an array into a stream, and then use the `min()` and `max()` functions to calculate the minimum and maximum values.

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.
