0% found this document useful (0 votes)
50 views

C Program To Find Second Smallest of N Elements With Given Complexity Constraint - Sanfoundry

This document discusses C programming examples and algorithms for graph problems, hard graph problems, computational geometry problems, sets and strings, and data structures. It then provides the source code for a C program to find the second smallest number in an array of integers. The program takes in user input for the size of the array and its elements, finds the smallest and second smallest elements, and prints out the second smallest number.

Uploaded by

Abhishek Lahoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

C Program To Find Second Smallest of N Elements With Given Complexity Constraint - Sanfoundry

This document discusses C programming examples and algorithms for graph problems, hard graph problems, computational geometry problems, sets and strings, and data structures. It then provides the source code for a C program to find the second smallest number in an array of integers. The program takes in user input for the size of the array and its elements, finds the smallest and second smallest elements, and prints out the second smallest number.

Uploaded by

Abhishek Lahoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

C - Graph Problems (http://www.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/)

« Prev Page (http://www.sanfoundry.com/c-program-compare-binary-sequential-search/)


Next Page (http://www.sanfoundry.com/c-program-implement-alexander-bogomolnys-unordered-permutation-algorithm-elements/) »

C Program to Find Second Smallest of n Elements with Given Complexity Constraint

This C program finds second smallest of n Elements in an array.

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. }

$ gcc secondsmallest.c -o secondsmallest


$ ./secondsmallest

How many numbers you want to enter: 10

Enter 10 numbers : 31 20 35 333 758 45 62 89 106 94

Second smallest element is 31

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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/)

« Prev Page - C Program to Compare Binary and Sequential Search (http://www.sanfoundry.com/c-program-compare-binary-sequential-search/)


» Next Page - C Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N (http://www.sanfoundry.com/c-program-
implement-alexander-bogomolnys-unordered-permutation-algorithm-elements/)

You might also like