Created
August 26, 2014 18:28
-
-
Save vinay13/a1f09bf09a2f291c63ef to your computer and use it in GitHub Desktop.
Find the smallest and second smallest element in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> /* For INT_MAX */ | |
/* Function to print first smallest and second smallest elements */ | |
void print2Smallest(int arr[], int arr_size) | |
{ | |
int i, first, second; | |
/* There should be atleast two elements */ | |
if (arr_size < 2) | |
{ | |
printf(" Invalid Input "); | |
return; | |
} | |
first = second = INT_MAX; | |
for (i = 0; i < arr_size ; i ++) | |
{ | |
/* If current element is smaller than first then update both | |
first and second */ | |
if (arr[i] < first) | |
{ | |
second = first; | |
first = arr[i]; | |
} | |
/* If arr[i] is in between first and second then update second */ | |
else if (arr[i] < second && arr[i] != first) | |
second = arr[i]; | |
} | |
if (second == INT_MAX) | |
printf("There is no second smallest element\n"); | |
else | |
printf("The smallest element is %d and second Smallest element is %d\n", | |
first, second); | |
} | |
/* Driver program to test above function */ | |
int main() | |
{ | |
int arr[] = {12, 13, 1, 10, 34, 1}; | |
int n = sizeof(arr)/sizeof(arr[0]); | |
print2Smallest(arr, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment