0
0
CProgramBeginner · 2 min read

C Program to Find Smallest Element in Array

To find the smallest element in an array in C, initialize a variable with the first element and then use a for loop to compare and update it if a smaller element is found, like if (arr[i] < min) min = arr[i];.
📋

Examples

Inputarr = {5, 3, 8, 1, 4}
OutputSmallest element is 1
Inputarr = {10, 20, 30, 40}
OutputSmallest element is 10
Inputarr = {-2, -5, -1, -9}
OutputSmallest element is -9
🧠

How to Think About It

To find the smallest element, start by assuming the first element is the smallest. Then check each element one by one. If you find an element smaller than your current smallest, update your smallest value. After checking all elements, the smallest value you have is the smallest element in the array.
📐

Algorithm

1
Get the number of elements in the array.
2
Set the first element as the smallest element.
3
Loop through the array starting from the second element.
4
Compare each element with the current smallest element.
5
If an element is smaller, update the smallest element.
6
After the loop ends, return or print the smallest element.
💻

Code

c
#include <stdio.h>

int main() {
    int arr[] = {5, 3, 8, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
    int min = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    printf("Smallest element is %d\n", min);
    return 0;
}
Output
Smallest element is 1
🔍

Dry Run

Let's trace the array {5, 3, 8, 1, 4} through the code to find the smallest element.

1

Initialize min

min = 5 (first element)

2

Compare arr[1] = 3 with min = 5

3 < 5, so min = 3

3

Compare arr[2] = 8 with min = 3

8 < 3? No, min stays 3

4

Compare arr[3] = 1 with min = 3

1 < 3, so min = 1

5

Compare arr[4] = 4 with min = 1

4 < 1? No, min stays 1

6

End of array

Smallest element found is 1

IndexCurrent ElementCurrent Min
055
133
283
311
441
💡

Why This Works

Step 1: Initialize minimum

We start by assuming the first element is the smallest using min = arr[0].

Step 2: Compare each element

We check each element with if (arr[i] < min) to find if there is a smaller value.

Step 3: Update minimum

If a smaller element is found, we update min to that element to keep track of the smallest value.

🔄

Alternative Approaches

Using a while loop
c
#include <stdio.h>

int main() {
    int arr[] = {5, 3, 8, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
    int min = arr[0];
    int i = 1;

    while (i < n) {
        if (arr[i] < min) {
            min = arr[i];
        }
        i++;
    }

    printf("Smallest element is %d\n", min);
    return 0;
}
Uses a while loop instead of for loop; functionally same but different style.
Sorting the array and picking first element
c
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr[] = {5, 3, 8, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    qsort(arr, n, sizeof(int), compare);

    printf("Smallest element is %d\n", arr[0]);
    return 0;
}
Sorts the array first; more overhead but useful if array needs sorting anyway.

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

Time Complexity

The program checks each element once in a single loop, so it runs in linear time O(n), where n is the number of elements.

Space Complexity

It uses only a few extra variables and does not require extra space proportional to input size, so space complexity is O(1).

Which Approach is Fastest?

The direct loop approach is fastest for just finding the smallest element. Sorting is slower (O(n log n)) but useful if you need the array sorted.

ApproachTimeSpaceBest For
Single loop comparisonO(n)O(1)Finding smallest element quickly
While loopO(n)O(1)Same as for loop, different style
Sorting then picking firstO(n log n)O(1)When sorted array is also needed
💡
Always initialize your smallest element variable with the first array element before looping.
⚠️
A common mistake is initializing the smallest element to zero or a fixed number instead of the first array element.