C program to find second smallest element in a one dimensional array

Here, we are implementing a C program that will read a one dimensional array of integers and find the second smallest element it. By Radib Kar, on December 05, 2018

Problem statement

Write a C program to find the second smallest element in a one dimensional array.

Examples

Example: Type 1: (all the elements are not same & no of element is more than two)

Input:
Array size: 4
Elements: 32 54 -6 -15

Output: 
-6

Example: Type 2: (second minimum doesn’t exist as size of array < 2)

Input:
Array size : 1
Elements: 4

Output: 
no second minimum

Example: Type 3: (all elements are same, thus only one minimum element)

Input:
Array size : 4
Elements: 3 3 3 3

Output: 
no second minimum

Algorithm to find second smallest element in a one dimensional array

  1. Define two variable min & sec_min
  2. Initialize min to array[0] & sec_min to INT_MAX
  3. Scan the entire array
  4. a. for(int i=0;i<n;i++)
        if array[i]<min
            then update min to array[i]&sec_min to previous min value
        else if array[i] is smaller than sec_min but greater than min
            then update sec_min to array value
            do nothing to min
    End for loop
    
  5. If still sec_min has the value INT_MAX
    Only one minimum value exists, print "No second minimum value exists"
    Else
    Print sec_min

C program to find second smallest element in a one dimensional array

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

int findSecondMin(int * a, int n) {
  if (n <= 1)
    return 0;

  //initialize max to first array element
  int min = a[0];
  //initialize min to first array element
  int sec_min = INT_MAX, temp = 0;

  for (int i = 0; i < n; i++) { //scan the entire array
    //if a[i]< min then update min to array value 
    //& second min to previous min value 
    if (a[i] < min) {
      sec_min = min;
      min = a[i];
    }
    //else if a[i] is smaller than second min but greater 
    //than min then update second min to array element value
    else if (a[i] < sec_min && a[i] > min)
      sec_min = a[i];
  }
  //if sec_min is still at its initialized value 
  //then no second minimum exists at all
  if (sec_min == INT_MAX)
    return 0;
  //else return second maximum
  return sec_min;
}

int main() {
  int n, sec_min = 0;

  printf("enter no of elements\n");
  scanf("%d", & n);

  //dynamic array created
  int * a = (int * ) malloc(sizeof(int) * n);

  printf("enter the elements........\n");
  //taking input
  for (int i = 0; i < n; i++)
    scanf("%d", & a[i]);

  //find second maximum value
  sec_min = findSecondMin(a, n);

  if (sec_min == 0) //if no second minimum exists
    printf("no second minimum exists\n");
  else
    printf("The second minimum no is : %d\n", sec_min);

  return 0;
}

Output

First run:
enter no of elements
4
enter the elements........
32 54 -6 -15
The second minimum no is : -6

Second run:
enter no of elements
1
enter the elements........
4
no second minimum exists

Third run:
enter no of elements
4
enter the elements........
3
3
3
3
no second minimum exists

To understand the above program, you should have the basic knowledge of the following C language topics:

C One-Dimensional Array Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.