Smallest Element in an Array

Definition & Explanation

This program aims to find the smallest element in a given array of integers. The task is to determine the minimum value present within the array. It employs a straightforward algorithm that iterates through the array, comparing each element with the current minimum value found. The program initializes a variable to store the smallest value with the first element of the array and then traverses the remaining elements, updating this variable whenever a smaller element is encountered.

Once all elements have been checked, the program outputs the smallest value found. This functionality is encapsulated within a function or method, making it reusable for different arrays. The program demonstrates the use of basic iteration and conditional statements to efficiently solve the problem of finding the smallest element in an array.

Program Logic

  1. Initialize Variables:
    • Create a variable min_val to store the smallest value found in the array.
    • Set min_val initially to the first element of the input array.
  2. Iterate Through the Array:
    • Start iterating through the input array from the second element onwards (index 1).
  3. Update Smallest Element:
    • For each element in the array:
      • Check if the current element is less than min_val.
        • If true, update min_val to the current element.
  4. Repeat:
    • Repeat step 3 for all elements in the array.
  5. Output:
    • Once the iteration is complete, the variable min_val will hold the smallest value found in the array.
    • Output this value as the result.
C

Method 1 :

#include <stdio.h>

int findSmallest(int arr[], int n) {
    if (n <= 0) {
        return 0; // Handle empty array case
    }

    int min_val = arr[0]; // Initialize min_val with first element

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

    return min_val;
}

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

    int smallest = findSmallest(arr, n);

    printf("Smallest: %d\n", smallest);

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <climits>

int findSmallest(int arr[], int n) {
    if (n <= 0) {
        return 0; // Handle empty array case
    }

    int min_val = arr[0]; // Initialize min_val with first element

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

    return min_val;
}

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

    int smallest = findSmallest(arr, n);

    std::cout << "Smallest: " << smallest << std::endl;

    return 0;
}

Output :

JAVA

Method 1 :

public class SmallestInArray {
    public static int findSmallest(int[] arr) {
        if (arr.length == 0) {
            return 0; // Handle empty array case
        }

        int minVal = arr[0]; // Initialize minVal with first element

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < minVal) {
                minVal = arr[i]; // Update minVal
            }
        }

        return minVal;
    }

    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 9, 2};

        int smallest = findSmallest(arr);

        System.out.println("Smallest: " + smallest);
    }
}

Output :

Python

Method 1 :

arr=[10,89,21,3,5]
mini=arr[0]

for i in range(len(arr)):
    if arr[i]<mini:
        mini=arr[i]


print(mini)

Output :

3