Program to find the second smallest element in an array

Select Articles

Program to find the second smallest element in an array

Finding the second smallest element in an array can be done in 3 different ways

They are:

Method 1: By sorting the array in ascending order and then displaying the second element.

Method 2: By traversing the array twice. In the first traversal find the smallest element (x) and in the second traversal, skip x and find the next smallest element which is >x.

Method 3: By traversing the array and storing the smallest element as and when we encounter it.

 

Method 1 to find the second smallest element in an array

An easier approach to finding the second smallest element in an array will be sorting the array completely and then returning the second element in the array.

 

Algorithm

Declare an array and input the array elements.

Sort the array (sort(arr, arr+n)).

Display the second element from the array.

 

Second smallest element in an array

C

#include

#include

#include

int main()

{

int n,i;

printf("\nEnter the number of elements : ");

scanf("%d",&n);

int arr[n];

printf("\nInput the array elements : ");

for(i = 0; i < n>

{

scanf("%d",&arr[i]);

}

int min = INT_MAX;

int second_min = INT_MAX ;

if (n < 2>

{

printf("\nInvalid Input");

}

for(i = 0; i < n>

{

if(arr[i] < min>

{

second_min = min;

min = arr[i];

}

}

for(i = 0; i < n>

{

if(arr[i] < second xss=removed>

{

second_min = arr[i];

}

}

printf("\nThe second smallest element is %d \n", second_min);

return 0;

}

 

C++

#include

using namespace std;

int main()

{

int min = INT_MAX;

int i,n = 5;

cout << "\nEnter the number of elements : ";

cin >> n;

int arr[n];

cout << "\nInput the array elements : ";

for(i = 0; i < n>

{

cin >> arr[i];

}

int second_min = INT_MAX ;

if (n < 2>

{

cout <<"\nInvalid Input";

}

for(i = 0; i < n>

{

if(arr[i] < min>

{

second_min = min;

min = arr[i];

}

}

for(i=0;i

{

if(arr[i] < second xss=removed>

{

second_min = arr[i];

}

}

cout << "\nThe second smallest element is " << second>

cout << endl>

return 0;

}

 

Output

Input - Enter the number of elements : 3 Input the array elements: 2 3 4 Output - The second smallest element is 3

 

Method 2 to find the second smallest element in an array

The second smallest element can be also found by traversing the array two times. In the first traversal, the smallest element (first_smallest) in the array is found and in the second traversal, the smallest element other than the first_smallest element is found.

 

Algorithm

Declare an array and input the array elements.

Find the smallest element (first_smallest) in the array in the first traversal.

Find the smallest element (second_smallest) by skipping the first_smallest element.

Display second_smallest.

 

C

#include

#include

#include

 

int main()

{

int n,i;

printf("\nEnter the number of elements : ");

scanf("%d",&n);

int arr[n];

printf("\nInput the array elements : ");

for(i = 0; i < n>

{

scanf("%d",&arr[i]);

}

int min = INT_MAX;

int second_min = INT_MAX ;

if (n < 2>

{

printf("\nInvalid Input");

}

for(i = 0; i < n>

{

if(arr[i] < min>

{

second_min = min;

min = arr[i];

}

}

for(i = 0; i < n>

{

if(arr[i] < second xss=removed>

{

second_min = arr[i];

}

}

printf("\nThe second smallest element is %d \n", second_min);

return 0;

}

 

C++

#include

using namespace std;

int main()

{

int min = INT_MAX;

int i,n = 5;

cout << "\nEnter the number of elements : ";

cin >> n;

int arr[n];

cout << "\nInput the array elements : ";

for(i = 0; i < n>

{

cin >> arr[i];

}

int second_min = INT_MAX ;

if (n < 2>

{

cout <<"\nInvalid Input";

}

for(i = 0; i < n>

{

if(arr[i] < min>

{

second_min = min;

min = arr[i];

}

}

for(i=0;i

{

if(arr[i] < second xss=removed>

{

second_min = arr[i];

}

}

cout << "\nThe second smallest element is " << second>

cout << endl>

return 0;

}

Output

Input- Enter the number of elements : 3 Input the array elements: 5 10 15 Output- The second smallest element is 10

 

Method 3 to find the second smallest element in an array

The most efficient method will be finding the second smallest element by traversing the array once.

 

Algorithm

Declare an array and input the array elements.

Initialize first_smallest = second_smallest = INT MAX;

Start traversing the array and assign the smallest element to first_smallest.

In the same traversal, if the next element is smaller than first_smallest, make that as first_smallest and make first_smallest as second_smallest.

Display second_smallest.

 

Second smallest element in an array

C

#include

#include

#include

int main()

{

int n,i;

printf("\nEnter the number of elements : ");

scanf("%d",&n);

int arr[n];

printf("\nInput the array elements : ");

for(i = 0; i < n>

{

scanf("%d",&arr[i]);

}

int min = INT_MAX;

int second_min = INT_MAX ;

if (n < 2>

{

printf("\nInvalid Input");

}

for(i = 0; i < n>

{

if(arr[i] < min>

{

second_min = min;

min = arr[i];

}

else if(arr[i] < second xss=removed>

{

second_min = arr[i];

}

}

printf("\nThe second smallest element is %d \n", second_min);

return 0;

}

 

C++

#include

using namespace std;

int main()

{

    int n,i;

    cout << "\nEnter the number of elements :  ";

    cin >> n;

    int arr[n];

    cout << "\nInput the array elements : ";

    for(i = 0; i < n>

    {

        cin >> arr[i];

    }

    int min = INT_MAX;

    int second_min = INT_MAX ;

    if (n < 2>

    {

        cout << "\nInvalid Input";

    }

    for(i = 0; i < n>

    {

        if(arr[i] < min>

        {

            second_min = min;

            min = arr[i];

        }

        else if(arr[i] < second xss=removed>

        {

            second_min = arr[i];

        }

    }

    cout << "\nThe second smallest element is "<< second>

    cout << endl>

    return 0;

}

Output

Input- Enter the number of elements : 3 Input the array elements: 5 10 15 Output- The second smallest element is 10