Start at the first element and update whenever a smaller value appears.
Calculation
min = 12
i=1 → 45 ≥ 12 → no change
i=2 → 7 < 12 → min = 7
i=3 → 42 ≥ 7 → no change
i=4 → 19 ≥ 7 → no change
Minimum = 7
Code
C Programs
Start with a simple running minimum. Then track the index when you also need the position.
Example 1 — Linear Scan
Keep a running minimum and update it whenever a smaller element appears.
C
#include <stdio.h>
int find_min(int arr[], int n)
{
int i, min;
if (n <= 0)
{
printf("Array must not be empty.\\n");
return 0;
}
min = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
return min;
}
int main(void)
{
int numbers[] = { 12, 45, 7, 42, 19 };
int n = (int)(sizeof numbers / sizeof numbers[0]);
printf("Minimum = %d\\n", find_min(numbers, n));
return 0;
}
Output
Minimum = 7
How It Works
Reject empty arrays (n <= 0) before reading arr[0].
Seed min with the first element.
Loop from index 1 so you never compare the seed against itself needlessly.
Update when arr[i] < min.
Return the final running minimum after one pass.
Example 2 — Track Index
Same scan, but also remember the first index where the minimum appears.
C
#include <stdio.h>
int find_min_index(int arr[], int n)
{
int i, min_index;
if (n <= 0)
{
printf("Array must not be empty.\\n");
return -1;
}
min_index = 0;
for (i = 1; i < n; i++)
{
if (arr[i] < arr[min_index])
{
min_index = i;
}
}
return min_index;
}
int main(void)
{
int numbers[] = { 12, 45, 7, 42, 19 };
int n = (int)(sizeof numbers / sizeof numbers[0]);
int index = find_min_index(numbers, n);
if (index >= 0)
{
printf("Min = %d at index %d\\n", numbers[index], index);
}
return 0;
}
Output
Min = 7 at index 2
How It Works
Start with min_index = 0 (first element is the candidate).
Compare each later element to arr[min_index].
When a smaller value appears, store that new index.
Ties keep the earlier index because the test uses strict <.
Read the value with numbers[index] after the scan.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
No minimum
Validate n > 0 before using arr[0].
Negatives
Still works
Do not seed with 0 — that fails for all-positive or all-negative arrays.
Duplicates
Same answer
Multiple copies of the min still return that value.
Sorting
Overkill
One scan is enough; sorting is usually slower for this goal.
Analysis
Time and Space Complexity
Program
Time
Extra space
Linear scan
O(n)
O(1)
Track index
O(n)
O(1)
You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted array.
Remember
Key Takeaways
Pattern: seed with arr[0], then scan with <.
Empty: validate n > 0 before reading the first element.
Index: same loop can track where the min sits.
Test: [12, 45, 7, 42, 19] → 7.
One line: one pass, keep the smaller value — that is the minimum.
Frequently Asked Questions
The first element is a valid candidate. Then every later element is compared against that running minimum.
An empty array has no minimum. Check that n > 0 before reading arr[0], then return early or print an error.
Yes. The minimum is still the smallest value. Seeding with arr[0] (not 0) also works for all-negative arrays.
No. Sorting is usually slower than one linear scan when you only need the minimum.
One pass over n elements: O(n) time and O(1) extra space.