Smallest Element in an Array: flowchart, algorithm and program
This program reads n numbers into an array and prints the smallest one. It assumes the first element is the smallest, then walks through the rest and updates that guess whenever it finds something smaller.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n.
- Step 3: Set i = 0.
- Step 4: If i < n, go to step 5; otherwise go to step 7.
- Step 5: Read arr[i].
- Step 6: Set i = i + 1. Go to step 4.
- Step 7: Set smallest = arr[0], i = 1.
- Step 8: If i < n, go to step 9; otherwise go to step 12.
- Step 9: If arr[i] < smallest, go to step 10; otherwise go to step 11.
- Step 10: Set smallest = arr[i].
- Step 11: Set i = i + 1. Go to step 8.
- Step 12: Print smallest.
- Step 13: Stop.
Pseudocode
BEGIN
READ n
i ← 0
WHILE i < n DO
READ arr[i]
i ← i + 1
END WHILE
smallest ← arr[0], i ← 1
WHILE i < n DO
IF arr[i] < smallest THEN
smallest ← arr[i]
END IF
i ← i + 1
END WHILE
PRINT smallest
END
Program in C, Python and Java
Smallest Element in an Array program in C
#include <stdio.h>
int main() {
int n, i, smallest;
int arr[100];
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
smallest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
printf("Smallest element = %d\n", smallest);
return 0;
}Smallest Element in an Array program in Python
n = int(input("Enter n: "))
arr = []
print("Enter", n, "numbers:")
for i in range(n):
arr.append(int(input()))
smallest = arr[0]
for i in range(1, n):
if arr[i] < smallest:
smallest = arr[i]
print("Smallest element =", smallest)Smallest Element in an Array program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter " + n + " numbers: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println("Smallest element = " + smallest);
}
}
Sample input: n = 5, numbers = 12, 7, 25, 3, 18
Output: Enter n: Enter 5 numbers: Smallest element = 3
How it works
- The first loop runs i from 0 to n-1 and uses scanf to store each input number in arr[i].
- Before checking anything, smallest = arr[0] so we start with a real value from the array.
- The second loop starts at i = 1 (arr[0] is already in smallest) and goes up to n-1.
- If arr[i] < smallest, we have found a new minimum, so smallest = arr[i]. Otherwise nothing changes.
- After the loop, smallest holds the minimum and is printed. For 12, 7, 25, 3, 18 it goes 12 -> 7 -> 3 and stays 3.
Dry run (trace table)
| Step | Line | Box | n | i | arr[0] | arr[1] | arr[2] | arr[3] | arr[4] | smallest | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||||||
| 2 | 8 | Read n | 5 | ||||||||
| 3 | 11 | i = 0 | 5 | 0 | |||||||
| 4 | 11 | i < n? | 5 | 0 | |||||||
| 5 | 12 | Read arr[i] | 5 | 0 | 12 | ||||||
| 6 | 11 | i = i + 1 | 5 | 1 | 12 | ||||||
| 7 | 11 | i < n? | 5 | 1 | 12 | ||||||
| 8 | 12 | Read arr[i] | 5 | 1 | 12 | 7 | |||||
| 9 | 11 | i = i + 1 | 5 | 2 | 12 | 7 | |||||
| 10 | 11 | i < n? | 5 | 2 | |||||||
| 11 | 12 | Read arr[i] | 5 | 2 | 25 | ||||||
| 12 | 11 | i = i + 1 | 5 | 3 | |||||||
| 13 | 11 | i < n? | 5 | 3 | |||||||
| 14 | 12 | Read arr[i] | 5 | 3 | 3 | ||||||
| 15 | 11 | i = i + 1 | 5 | 4 | |||||||
| 16 | 11 | i < n? | 5 | 4 | |||||||
| 17 | 12 | Read arr[i] | 5 | 4 | 18 | ||||||
| 18 | 11 | i = i + 1 | 5 | 5 | |||||||
| 19 | 11 | i < n? | 5 | 5 | |||||||
| 20 | 15 | smallest = arr[0], i = 1 | 5 | 1 | 12 |
Time and space complexity
Time: O(n) · Space: O(n). Each element is read once and compared once, and the array stores n elements.
Common mistakes
- Starting smallest at 0 instead of arr[0]. If all the numbers are positive, the answer would wrongly stay 0.
- Starting the second loop at i = 0 or running it to i <= n, which reads arr[n], outside the numbers you entered.
- Using > instead of < in the comparison, which finds the largest element instead of the smallest.
Viva questions
Q1. Why do you initialise smallest with arr[0] instead of 0?
Because 0 might be smaller than every element (for example if all numbers are positive), which would give a wrong answer. Starting with arr[0] guarantees the minimum is an actual element of the array.
Q2. Why does the second loop start from i = 1 and not i = 0?
arr[0] is already stored in smallest, so comparing it with itself is unnecessary. Starting at 1 saves one comparison and the result stays the same.
Q3. Explain what the condition arr[i] < smallest checks and what happens when it is true.
It checks whether the current element is smaller than the smallest value seen so far. If true, smallest is updated to arr[i], so after the loop it holds the minimum.
Q4. What happens if the user enters n = 1?
The second loop does not run because i = 1 is not less than 1. smallest stays arr[0], which is correct as the only element is the smallest.
Q5. What happens if n is 0 or negative, or greater than 100? Does the program handle it?
No, it does not. For n = 0 or negative, arr[0] is read without being set, so the result is garbage. For n > 100, scanf writes beyond the array, causing undefined behaviour. We should validate n first (1 <= n <= 100).
Q6. Does this program work with negative numbers and duplicate values?
Yes. It only uses the < comparison, so negatives are handled correctly. With duplicates, the value is still the minimum, since equal values do not change smallest.
Q7. What is the time and space complexity of this algorithm?
Time is O(n) because the array is scanned once with about n-1 comparisons. Extra space is O(1) as only a few variables are used, besides the array itself.
Q8. What would you change to also print the position (index) of the smallest element?
Keep an integer variable pos initialised to 0, and whenever arr[i] < smallest, set pos = i along with updating smallest. Then print pos at the end. To find the largest instead, change the condition to arr[i] > largest.
Frequently asked questions
Why do we set smallest = arr[0] first?
It gives the comparison a starting value that is actually in the array, so the result is correct even when all numbers are negative or all are large.
What happens if n is 1?
The second loop does not run, since i starts at 1 and 1 < 1 is false. smallest stays arr[0], which is the only element, so it is printed correctly.
How do I find the largest element instead?
Change the condition to arr[i] > largest and rename the variable. The rest of the logic is the same.
What is the time complexity?
O(n), because each element is looked at once. It needs O(1) extra space apart from the array.