C Program to find the smallest element in an array
Learn how to find the smallest element in an array in C using a single traversal, with source code, examples, a dry run, and complexity analysis.
To find the smallest element in an array, compare every element with the smallest value found so far. If the current element is smaller, save it as the new smallest value.
For example, in the array:
12 -4 27 -9 19
the smallest element is -9.
C Program to Find the Smallest Array Element
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
int smallest = array[0];
for (int index = 1; index < size; index++) {
if (array[index] < smallest) {
smallest = array[index];
}
}
printf("Smallest element = %d\n", smallest);
return 0;
}
Sample Output
Enter the number of elements: 5
Enter 5 elements:
12 -4 27 -9 19
Smallest element = -9
How the Program Works
- The program reads and validates the number of array elements.
- The first loop stores the values entered by the user.
smallestis initialized with the first element,array[0].- The comparison loop visits the remaining elements from index
1to indexsize - 1. - When an element is less than
smallest, that element becomes the new smallest value. - After the final comparison,
smallestcontains the minimum array element.
Here is a dry run for 12, -4, 27, -9, 19:
| Current element | Smallest before comparison | Smallest after comparison |
|---|---|---|
| -4 | 12 | -4 |
| 27 | -4 | -4 |
| -9 | -4 | -9 |
| 19 | -9 | -9 |
Why Initialize smallest with the First Element?
Setting smallest to 0 would fail for an array containing only positive numbers. For example, the smallest element in 8, 3, 12 is 3, but 0 would remain unchanged even though it does not appear in the array.
Initializing with array[0] ensures that the result is always one of the actual array elements. It also works correctly for negative numbers and zero.
Using a Function to Find the Smallest Element
The comparison logic can be placed in a reusable function:
#include <stdio.h>
int findSmallest(const int array[], int size) {
int smallest = array[0];
for (int index = 1; index < size; index++) {
if (array[index] < smallest) {
smallest = array[index];
}
}
return smallest;
}
int main(void) {
int numbers[] = {18, 6, 23, 9};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Smallest element = %d\n", findSmallest(numbers, size));
return 0;
}
Output
Smallest element = 6
The array parameter is marked const because the function only examines the elements. The function requires an array with at least one element so that accessing array[0] is valid.
Time and Space Complexity
- Time complexity:
O(n), because the program examines every array element once. - Extra space complexity:
O(1), because it uses only a loop index and a variable for the smallest value.
Common Mistakes
- Initializing
smallestto0, which fails when all elements are positive. - Using
>instead of<, which finds the largest element instead. - Starting with an arbitrary fixed value that may be smaller than every array element.
- Allowing
sizeto be zero and then accessingarray[0]. - Sorting the array to find its smallest element when a single traversal is enough.
This approach finds the smallest element efficiently without rearranging or otherwise changing the original array.