# Smallest Element in an Array in Python

## Smallest Element in an Array in Python

Given an array of integers “A”, the task is to write a Program for Finding the Smallest Element in an Array in Python using recursion algorithm.

For instance,

```Input: String = [1, 4, 3, -5, -4, 8, 6];
Output: -5``` ### Smallest Element in an Array

The objective is to recursively traverse the whole input array and compare each characters to find the smallest of them all. In order to do so we declare a function and pass the original array and it’s Length. With each recursive step call we’ll keep on decrementing the value of length “n” as shown in the image adjacent. The base condition for the function is given as such that the recursion terminates when the length “n”  becomes  1. as shown in the last block in the image.

Let’s understand the whole process in more depth by taking an example. Let the original array be A = [1, 4, 3, -5]. Now to recursively call the function giving the array “A” and it’s length “n” as arguments. The function returns the minimum of the last element of the array “A” and the value returned by the recursive call of the function findMinRec(A,n-1) using min() function.

Once the function is called recursively, it goes on breaking down the string into tiny bits. The recursion is terminated when the base base is satisfied. It’ll return the first element of the array A. The values are then compared as shown in the image and the minimum value is returned after comparing. #### Python Code

Run
```def findMinRec(A, n):
if (n == 1):
return A
return min(A[n - 1], findMinRec(A, n - 1))

# Driver Code
if __name__ == '__main__':
A = [1, 4, 45, 6, -50, 10, 2]
n = len(A)
print(findMinRec(A, n))
```

#### Output

```-50
```

Explanation

The objective is to recursively parse through the given Array input and print the smallest number among all the Array input.

The algorithm for the above code is as follows:

1. Define a function findMinRec() which accepts the array “A” and it’s length “n” as arguments.
2. Checking if the length “n” is 1. if true return the first element of array “A”.
3. Else Recursively call the the findMinRec() function and return the minimum of A[n-1] and findMinrec(A,n-1) using min() function.
4. Initialize the variables and call the findMinRec() function from the driver code.
5. Print the output using print() function.

The output for the code is the smallest element in the array input i.e -50.

For more approaches and solutions to the above problem, click the buttons.