0
0
CppProgramBeginner · 2 min read

C++ Program to Find Smallest Element in Array

To find the smallest element in an array in C++, use a loop to compare each element with a variable holding the current smallest value, like int min = arr[0]; for (int i = 1; i < n; i++) if (arr[i] < min) min = arr[i];.
📋

Examples

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

How to Think About It

To find the smallest number, start by assuming the first element is the smallest. Then check each other element one by one. If you find a smaller number, update your smallest number. At the end, the smallest number you have is the smallest in the whole array.
📐

Algorithm

1
Start with the first element as the smallest.
2
Go through each element in the array from the second to the last.
3
Compare the current element with the smallest found so far.
4
If the current element is smaller, update the smallest value.
5
After checking all elements, return the smallest value.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5};
    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];
        }
    }
    cout << "Smallest element is " << min << endl;
    return 0;
}
Output
Smallest element is 1
🔍

Dry Run

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

1

Initialize min

min = 3 (first element)

2

Compare arr[1] = 1 with min = 3

1 < 3, so min = 1

3

Compare arr[2] = 4 with min = 1

4 < 1? No, min stays 1

4

Compare arr[3] = 1 with min = 1

1 < 1? No, min stays 1

5

Compare arr[4] = 5 with min = 1

5 < 1? No, min stays 1

6

End of array

Smallest element found is 1

IndexCurrent ElementCurrent Min
033
111
241
311
451
💡

Why This Works

Step 1: Start with first element

We assume the first element is the smallest to have a starting point for comparison.

Step 2: Compare each element

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

Step 3: Update smallest value

When a smaller element is found, we update min to keep track of the smallest number.

Step 4: Result after loop

After checking all elements, min holds the smallest element in the array.

🔄

Alternative Approaches

Using std::min_element from <algorithm>
cpp
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int min = *min_element(arr, arr + n);
    cout << "Smallest element is " << min << endl;
    return 0;
}
This uses a built-in function for simplicity and readability but requires including <algorithm>.
Sorting the array and taking first element
cpp
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n);
    cout << "Smallest element is " << arr[0] << endl;
    return 0;
}
Sorting is less efficient for just finding the smallest element but useful if you need the array sorted.

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

Time Complexity

The program checks each element once, so the time grows linearly with the array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The simple loop approach is fastest for just finding the smallest element. Using sorting is slower (O(n log n)), and std::min_element is optimized but similar to the loop.

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Finding smallest element quickly
std::min_elementO(n)O(1)Cleaner code with standard library
Sorting arrayO(n log n)O(1)When sorted array is also needed
💡
Always initialize your smallest value with the first element of the array before comparing.
⚠️
A common mistake is starting the smallest value as zero or another number instead of the first array element, which can give wrong results.