# Programming Videos

## Search

### C Program to Find Second Smallest Number in an Array without Sorting

Write a C program to find second smallest number in an array without sorting. Given an unsorted array of integer, we have to write a code to find second smallest number without using sorting.

We can solve this problem easily using sorting. First we sort an array and then we pick the element at 2nd index which is the second smallest element. This sorting technique won't work if the element of an array is repeated.  Think for a moment how do we solve this problem without using sorting.

Find second smallest number in an array using sorting

Sorting algorithm and their time complexity

## Algorithm To Find Second Smallest Number in an Array without Sorting

1. Take two variables smallest and secondSmallest and intialized with INT_MAX (maximum integer value).

2. Run a loop,

a) If value is smaller than smallest, then update smallest and secondSmallest.
b) if the current element is smaller than secondSmallest then update  secondsmallest.

Program to Find Smallest Number Without Using Comparison Operator

Program to Find Smallest Number in An Array

## C Program to Find Second Smallest Number in an Array without Sorting

We have discussed the logic to find second smallest number in an array. Let's write a c code to implement them programatically.

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

int main() {

int arr, n, i;

//Enter the size of an array
printf("Enter the size of an array (Max 50) \n");
scanf("%d", &n);

printf("Enter an array elements\n");

//Input array values
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

//Intialize variable with max int value
int smallest = INT_MAX;
int secondSmallest = INT_MAX;

//Traverse an array
for(i = 0; i < n; i++) {

//If element is smaller
if(arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
}

if(arr[i] > smallest && arr[i] < secondSmallest) {
secondSmallest = arr[i];
}
}

printf("Second smallest %d", secondSmallest);

return 0;
}

```

Output :

Enter the size of an array (Max 50) : 5
Enter an array elements
-1
2
8
3
4
Second smallest 2