Program to find Smallest element in an array using C

Smallest Element in an array in C

Here, in this page we will discuss the program to find the smallest element in an array in C programming language. We apply algorithm that assumes the first element as smallest element and then compare it with other elements if an element is smaller than it then, it becomes the new smallest element, and this process is repeated till complete array is scanned.

Smallest element in an array in C

Methods Covered

  • Method 1: Simple iterative
  • Method 2: Top Down recursive
  • Method 3: Bottom Up Recursive

 

Method 1:-

For a user-defined input num:-

  1. Assign min = arr[0]
  2. Linearly traverse through the whole array
  3. Whenever you encounter a smaller element (arr[i] < min)
  4. Update min, min = arr[i]

C Program:-

Run
#include <stdio.h>

int getSmallest(int arr[], int len)
{
    // assign first array element as smallest
    int min = arr[0];
    
    // linearly search for the smallest element
    for(int i=1; i < len; i++)
    {
        // if the current array element is smaller
        if (arr[i] < min)
            min = arr[i];
    }
    
    return min;
    
}
int main()
{
    int arr[] = {5, 8, 7, 2, 12, 4};
    
    // get the length of the array
    int len = sizeof(arr)/sizeof(arr[0]);    
    
    printf("The smallest : %d", getSmallest(arr, len));
}

Output

The smallest : 2

Method 2 (Using Recursion)

This method requires you to know recursion in C.

Call a function : getSmallest(int arr[], int i, int len, int max)

  1. With initial call up values as : getSmallest(arr, 0, len, arr[0])
    1. Initially passing min as arr[0]
    2. Initially passing ‘i’ as 0
  2. In each recursive call check if current array element is smaller than min
  3. If it is then assign min = arr[i]
  4. Move to the next recursive call trying to do the same thing for the next array index
  5. Stop when you’ve read all elements and reach the bounds of array and print max value

Method 2 C Program:-

Run
// C Program to find smallest element in an array
// using top down recursive approach
#include <stdio.h>

void getSmallest(int arr[], int i, int len, int min)
{
    // base case, when last element was read in previous recursion 
    if(i >= len){
        printf("Smallest: %d", min);
        return;
    }
    
    if(arr[i] < min)
        min = arr[i];
    
    getSmallest(arr, i+1, len, min);
}
int main()
{
    int arr[] = {30, 5, 20, 60, 10, 50, 25};
    
    // get the length of the array
    int len = sizeof(arr)/sizeof(arr[0]);    
    
    getSmallest(arr, 0, len, arr[0]);
    
    return 0;
}
// Time complexity: O(N)
// Space complexity: O(1)
// Auxilary space complexity : O(N) due to function call stack

Output

Smallest: 5

Method 3 (Using Recursion)

This method requires you to know recursion in C

Call a function : minimum(int arr[], int i, int end)

  1. With initial call up values as minimum(arr, 0, end)
    1. Initially passing end as the index of last array element
    2. Initially passing ‘i’ as 0
  2. Recursively reach iteration where reading 2nd last element
  3. Find smaller between 2nd last and last array elements
  4. And return the result to min value of the previous recursive iteration
  5. In each of the remaining recursive callups find the smaller b/w current array index element and current min value
  6. Pass final min value from last recursive callup to main and print

Method 3 C Program:-

Run
// C Program to find smallest element in an array
// using bottom up recursive approach
#include <stdio.h>

int minimum(int arr[], int i, int end)
{
    int min;
    
    // when we reach 2nd last array element
    // return the smaller b/w last & 2nd last elements
    if(i == end-1)
        return (arr[i] < arr[i + 1]) ? arr[i] : arr[i + 1];

    min = minimum(arr, i + 1, end);

    return (arr[i] < min) ? arr[i] : min;
}

int main()
{
    int arr[] = {25, 10, 40, 30, 20, 80};
    
    // storing the index of end array element 
    // end = 5 : arr[5] = 80 (the end element)
    int end = sizeof(arr)/sizeof(arr[0]) - 1;    

    int min = minimum(arr, 0, end);

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

    return 0;
}

Output

Smallest: 10

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Program to find Smallest element in an array using C”


  • Ankit

    In short if we gave to code this. Here is the code
    import numpy as np
    a= np.array([10,9,4,6,20,25])
    print(a.min())


  • Ankit

    In python if we take user defined input array and then check the smallest of all elements. Here is the program
    from array import*
    arr=array(‘i’,[])
    n=int(input(“enter the length of array”))
    for i in range(n):
    print (i+1)
    x=int(input(“ENTER THE ELEMENT”))
    arr.append(x)
    print(arr)
    def smallest (arr,n):
    min=arr[0]
    for i in range(1,n):
    if arr[i]<min:
    min=arr[i]
    return min
    print("The smallest element is: ", smallest(arr,n))