Find Second Smallest Element in an Array using C

Find Second Smallest Element in an Array using Python

Second Smallest Element

Today we learn how to find second smallest element in an array with help of C Programing concept. First of all we will sort the array in ascending order then automatically the second smallest element would be at the index 1.
For Example:
Input : array = [11, 15, 2, 4, 91, 10, 20, 16, 14]
Output :
Second Smallest element is: 4

Working Required for finding second smallest element entered by the user

Step 1: Initialize the array
Step 2: Input the elements from the user
Step 3: Sort the array in the ascending order
Step 4: Second Smallest element would be at the index 1.
Step 5: Print second smallest number.

C Code for finding Second Smallest Element in an Array

#include <stdio.h>
int main()
{
    int n;
    int i, j, temp;
    scanf("%d",&n);
    int array[n];
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(array[i]>array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    printf("Second Smallest Element of the array is : %d",array[1]);
    return 0;
}

9
11 15 2 4 91 10 20 16 14
Second Smallest Element of the array is 4