Find the Smallest Element in an Array

smallest element

The smallest element in an array is the element that has the minimum value. For example if an array has the element set {20, 30, 10, 40, 70, 100}, then the smallest element is 20. We have to check each element for an unsorted array to find the smallest element.






1. Algorithm to find smallest element in an array


1. Take input of an array A[] and the size n

2. Initially set the first element as minimum element i.e. min = A[0]

3. Set i = 0

4. Check if min > A[i]

5. If step 4 is true set min = A[i] and i = i + 1.

6. If step 4 is false set i = i + 1.

7. If i < n, go to step 4 else display min as the smallest element.





2. Pseudocode to find smallest element in an array


Input: An array $A[~]$ and size of array $n$

Output: smallest element in $A[~]$

1. Procedure smallestInArray($A[~]$, $n$):

2. $min \leftarrow A[0]$

3. Repeat for $i \in [0, n - 1]$:

4. If $min > A[i]$:

5. $min = A[i]$

6. Return $min$

7. End Procedure





3. Time complexity to find smallest element in an array


Time Complexity: O(n)

Where $n$ is the total no of elements in the array.





4. Program & output to find smallest element in an array using Iteration

In this section, the iterative programs to find the smallest element in an array are given.




4.1. C Program & output to find smallest element in an array using Iteration

Code has been copied
/********************************
       alphabetacoder.com
C program to find smallest element 
in an array using iteration
*********************************/

#include <stdio.h>

int main() {
    // declare an array
    int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
    // declare variables
    int i, min;

    // set the first element as the min element
    min = arr[0];

    // compare each element in array 
    // to find the smallest element
    for (i = 0; i < 8; i++) {
        if (min > arr[i])
            min = arr[i];
    }

    // display result
    printf("%d is the smallest element!", min);

    return 0;
}

Output


2 is the smallest element!




4.2. C++ Program & output to find smallest element in an array using Iteration

Code has been copied
/***********************************
       alphabetacoder.com
C++ program to find smallest element 
in an array using iteration
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare an array
    int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
    // declare variables
    int i, min;

    // set the first element as the min element
    min = arr[0];

    // compare each element in array 
    // to find the smallest element
    for (i = 0; i < 8; i++) {
        if (min > arr[i]) {
            min = arr[i];
        }
    }

    // display result
    cout << min << " is the smallest element!";

    return 0;
}

Output


2 is the smallest element!




4.3. Java Program & output to find smallest element in an array using Iteration

Code has been copied
/***********************************
       alphabetacoder.com
Java program to find the smallest
element in an array using iteration
************************************/

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // declare an array
        int arr[] = {2, 50, 7, 23, 12, 17, 9, 10};
        // declare variables
        int i, min;

        // set the first element as the min element
        min = arr[0];

        // compare each element in array 
        // to find the smallest element
        for (i = 0; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }

        // display result
        System.out.println(min + " is the smallest element!");
    }
}

Output


2 is the smallest element!




4.4. Python Program & output to find smallest element in an array using Iteration

Code has been copied
# ************************************
#        alphabetacoder.com
# Python program to find the smallest
# element in an array using iteration
# ************************************

# declare a list
arr = [2, 50, 7, 23, 12, 17, 9, 10]

# set the first element as the min element
minimum = arr[0]

# compare each element in array
# to find the minimum element
for i in range(len(arr)):
    if minimum > arr[i]:
        minimum = arr[i]

# display result
print(minimum, "is the smallest element!")

Output


2 is the smallest element!




4.5. C# Program & output to find smallest element in an array using Iteration

Code has been copied
/***********************************
       alphabetacoder.com
C# program to find the smallest
element in an array using iteration
************************************/
using System;

namespace SmallestElement {
    class Program {
        static void Main(string[] args) {
            // declare an array
            int[] arr = {2, 50, 7, 23, 12, 17, 9, 10};
            // declare variables
            int i, min;

            // set the first element as the min element
            min = arr[0];

            // compare each element in array 
            // to find the minimum element
            for (i = 0; i < 8; i++) {
                if (min > arr[i]) {
                    min = arr[i];
                }
            }

            // display output
            Console.WriteLine(min + " is the smallest element!");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


2 is the smallest element!




5. Program & output to find smallest element in an array using recursion

In this section, the recursive programs to find the smallest element in an array are given.




5.1. C Program & output to find smallest element in an array using recursion

Code has been copied
/********************************
       alphabetacoder.com
C program to find smallest element 
in an array using recursion
*********************************/

#include <stdio.h>

// recursive function to find
// smallest element in array
int find_smallest(int arr[], int smallest, int i){
    // exit condition
    if(i < 0)
	return smallest;
    // compare smallest with 
    // current element
    if(smallest >= arr[i])
	smallest = arr[i];
    // call function
    return find_smallest(arr, smallest, i - 1);
} 

int main() {
    // declare an array
    int arr[8] = {2, 50, 1, 23, 12, 17, 9, 10};
    // declare variables
    int min, length;
    
    // calculate size of array
    length = sizeof(arr)/sizeof(arr[0]);

    // call function to get the 
    // smallest element as return value
    // pass the array, first element as 
    // default smallest element and the
    // size of the array
    min = find_smallest(arr, arr[0], length - 1);
    
    // display result
    printf("%d is the smallest element!", min);

    return 0;
}

Output


1 is the smallest element!




5.2. C++ Program & output to find smallest element in an array using recursion

Code has been copied
/***********************************
       alphabetacoder.com
C++ program to find smallest element 
in an array using recursion
************************************/

#include <iostream>

using namespace std;

// recursive function to find
// smallest element in array
int find_smallest(int arr[], int smallest, int i) {
    // exit condition
    if (i < 0)
        return smallest;
    // compare smallest with 
    // current element
    if (smallest >= arr[i]) {
        smallest = arr[i];
    }
    // call function
    return find_smallest(arr, smallest, i - 1);
}

int main() {
    // declare an array
    int arr[8] = {2, 50, 1, 23, 12, 17, 9, 10};
    // declare variables
    int min, length;

    // calculate size of array
    length = sizeof(arr) / sizeof(arr[0]);

    // call function to get the 
    // smallest element as return value
    // pass the array, first element as 
    // default smallest element and the
    // size of the array
    min = find_smallest(arr, arr[0], length - 1);

    // display result
    cout << min << " is the smallest element!";

    return 0;
}

Output


1 is the smallest element!




5.3. Java Program & output to find smallest element in an array using recursion

Code has been copied
/***********************************
       alphabetacoder.com
Java program to find the smallest 
element in an array using recursion
************************************/

import java.util.Scanner;

public class Main {

    // recursive function to find
    // smallest element in array
    public static int find_smallest(int arr[], int smallest, int i) {
        // exit condition
        if (i == arr.length)
            return smallest;
        // compare smallest with 
        // current element
        if (smallest > arr[i])
            smallest = arr[i];
        // call function
        return find_smallest(arr, smallest, i + 1);
    }

    public static void main(String[] args) {
        // declare an array
        int arr[] = {2, 50, 7, 23, 12, 17, 9, 10};
        // declare variables
        int min;

        // call function to get the 
        // smallest element as return value
        // pass the array, first element as 
        // default smallest element and the
        // size of the array
        min = find_smallest(arr, arr[0], 0);

        // display result
        System.out.println(min + " is the smallest element!");
    }
}

Output


2 is the smallest element!




5.4. Python Program & output to find smallest element in an array using recursion

Code has been copied
# ************************************
#        alphabetacoder.com
# Python program to find the smallest
# element in an array using recursion
# ************************************


# recursive function
def find_smallest(arr, smallest, i):
    # exit condition
    if i == len(arr):
        return smallest

    # compare the smallest with
    # the current element
    if smallest >= arr[i]:
        smallest = arr[i]

    # call function
    return find_smallest(arr, smallest, (i + 1))


def main():
    # declare a list
    arr = [2, 50, 7, 23, 12, 17, 9, 10]

    # call function to get the
    # smallest element as return value
    smallest = find_smallest(arr, arr[0], 0)

    # display result
    print(smallest, "is the smallest element!")


# call main() function
main()

Output


2 is the smallest element!




5.5. C# Program & output to find smallest element in an array using recursion

Code has been copied
/**********************************
       alphabetacoder.com
C# program to find the smallest
element in an array using recursion
***********************************/
using System;

namespace SmallestElement {
    class Program {
        // recursive function to find
        // smallest element in array
        public static int find_smallest(int[] arr, int smallest, int i) {
            // exit condition
            if (i == arr.Length)
                return smallest;
            // compare smallest with 
            // current element
            if (smallest >= arr[i])
                smallest = arr[i];
            // call function
            return find_smallest(arr, smallest, i + 1);
        }

        static void Main(string[] args) {
            // declare an array
            int[] arr = {2, 50, 7, 23, 12, 17, 9, 10};
            // declare variables
            int min = 0;

            // call function to get the min element
            min = find_smallest(arr, arr[0], 0);

            // display output
            Console.WriteLine(min + " is the smallest element!");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


2 is the smallest element!




6. Find the smallest element in an array using function

In this section, the programs using the inbuilt functions to find the smallest element in an array are given.




6.1. C++ Program & output to find the smallest element in an array using STL function min_element()

Code has been copied
/**************************************
       alphabetacoder.com
C++ program to find the smallest element 
in an array using STL function
***************************************/

#include <iostream>

#include<algorithm>

using namespace std;

int main() {
    // declare an array
    int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
    // declare variables
    int min;

    // get the smallest element using function
    max = * min_element(arr, arr + 8);

    // display result
    cout << min << " is the smallest element!";

    return 0;
}

Output


2 is the smallest element!




6.2. Python Program & output to find the smallest element in an array using min() function

Code has been copied
# ***************************************
#        alphabetacoder.com
# Python program to find the smallest
# element in a list using min() function
# ***************************************

# declare a list
arr = [2, 50, 7, 23, 12, 17, 9, 10]

# get the minimum element using function
minimum = min(arr)

# display result
print(minimum, "is the smallest element!")

Output


2 is the smallest element!