C Program To Find Second Smallest of N Elements With Given Complexity Constraint - Sanfoundry
C Program To Find Second Smallest of N Elements With Given Complexity Constraint - Sanfoundry
com/c-programming-examples-graph-problems-algorithms/)
C - Hard Graph Problems (http://www.sanfoundry.com/c-programming-examples-hard-graph-problems-algorithms/)
C - Computation Geometry (http://www.sanfoundry.com/c-programming-examples-computational-geometry-problems-algorithms/)
C - Sets & Strings (http://www.sanfoundry.com/c-programming-examples-set-string-problems-algorithms/)
C - Data-Structures (http://www.sanfoundry.com/c-programming-examples-data-structures/)
Given an array of n Integers, this algorithm finds the second smallest element out of it.
Here is the source code of the C program to find the second smallest number. The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. #include <stdio.h>
2. #include <string.h>
3.
4. main()
5. {
6. int smallest, secondsmallest;
7. int array[100], size, i;
8. printf("\n How many elements do you want to enter: ");
9. scanf("%d", &size);
10. printf("\nEnter %d elements: ", size);
11. for (i = 0 ; i < size; i++)
12. scanf("%d", &array[i]);
13. if (array[0] < array[1]) {
14. smallest = array[0];
15. secondsmallest = array[1];
16. }
17. else {
18. smallest = array[1];
19. secondsmallest = array[0];
20. }
21. for (i = 2; i < size; i++) {
22. if (array[i] < smallest) {
23. secondsmallest = smallest;
24. smallest = array[i];
25. }
26. else if (array[i] < secondsmallest) {
27. secondsmallest = array[i];
28. }
29. }
30. printf(" \nSecond smallest element is %d", secondsmallest);
31. }
Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms. (http://www.sanfoundry.com/best-reference-books-c-programming-data-
structures-algorithms/)