Python Program to Find Minimum in Array
min() function like min_value = min(array).Examples
How to Think About It
Algorithm
Code
array = [3, 1, 4, 1, 5] min_value = min(array) print(min_value)
Dry Run
Let's trace the array [3, 1, 4, 1, 5] through the code.
Initialize minimum
Start with min_value = 3 (first element)
Compare with 1
1 is less than 3, update min_value to 1
Compare with 4
4 is not less than 1, min_value stays 1
Compare with 1
1 is equal to min_value, no change
Compare with 5
5 is not less than 1, min_value stays 1
Result
Minimum value found is 1
| Current Element | Current Minimum |
|---|---|
| 3 | 3 |
| 1 | 1 |
| 4 | 1 |
| 1 | 1 |
| 5 | 1 |
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
array = [3, 1, 4, 1, 5] min_value = array[0] for num in array: if num < min_value: min_value = num print(min_value)
array = [3, 1, 4, 1, 5] sorted_array = sorted(array) print(sorted_array[0])
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().
| Approach | Time | Space | Best For |
|---|---|---|---|
| min() function | O(n) | O(1) | Quick and simple minimum search |
| Manual loop | O(n) | O(1) | Learning how minimum search works |
| sorted() function | O(n log n) | O(n) | When sorted order is also needed |
min() function for the simplest and fastest way to find the minimum.min().