0
0
PythonProgramBeginner · 2 min read

Python Program to Find Minimum in Array

You can find the minimum value in an array in Python using the built-in min() function like min_value = min(array).
📋

Examples

Input[3, 1, 4, 1, 5]
Output1
Input[10, 20, 30, 40]
Output10
Input[-5, -10, 0, 5]
Output-10
🧠

How to Think About It

To find the smallest number in an array, look at each number one by one and remember the smallest you have seen so far. At the end, the remembered number is the minimum.
📐

Algorithm

1
Start with the first element as the minimum.
2
Go through each element in the array.
3
If you find an element smaller than the current minimum, update the minimum.
4
After checking all elements, return the minimum value.
💻

Code

python
array = [3, 1, 4, 1, 5]
min_value = min(array)
print(min_value)
Output
1
🔍

Dry Run

Let's trace the array [3, 1, 4, 1, 5] through the code.

1

Initialize minimum

Start with min_value = 3 (first element)

2

Compare with 1

1 is less than 3, update min_value to 1

3

Compare with 4

4 is not less than 1, min_value stays 1

4

Compare with 1

1 is equal to min_value, no change

5

Compare with 5

5 is not less than 1, min_value stays 1

6

Result

Minimum value found is 1

Current ElementCurrent Minimum
33
11
41
11
51
💡

Why This Works

Step 1: Using min() function

The min() function checks all elements and returns the smallest one automatically.

Step 2: Iterating elements manually

By comparing each element with the current minimum, we keep track of the smallest number found so far.

Step 3: Final minimum value

After checking all elements, the smallest number is the minimum value in the array.

🔄

Alternative Approaches

Manual loop to find minimum
python
array = [3, 1, 4, 1, 5]
min_value = array[0]
for num in array:
    if num < min_value:
        min_value = num
print(min_value)
This method shows how to find minimum without built-in functions, useful for learning or custom logic.
Using sorted() function
python
array = [3, 1, 4, 1, 5]
sorted_array = sorted(array)
print(sorted_array[0])
Sorting the array and taking the first element gives the minimum but is less efficient than min().

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so time grows linearly with array size.

Space Complexity

Only a few variables are used, so extra memory stays constant regardless of input size.

Which Approach is Fastest?

Using min() is fastest and simplest; sorting is slower due to extra work; manual loop is educational but similar in speed to min().

ApproachTimeSpaceBest For
min() functionO(n)O(1)Quick and simple minimum search
Manual loopO(n)O(1)Learning how minimum search works
sorted() functionO(n log n)O(n)When sorted order is also needed
💡
Use the built-in min() function for the simplest and fastest way to find the minimum.
⚠️
Forgetting to handle empty arrays, which causes errors when calling min().