@Dave, I check for the values you suggested but the code worked fine. There
were other errors in the code. I have rectified them now.
The following code seems to be working fine and in O(n) time.
#include <stdio.h>
#include <stdlib.h>
void find_two_mins(int array[], int size)
{
int i,t;
int min1, min2;
if(array[0] <= array[1]) {
min1=array[0];
min2=array[1];
} else {
min2=array[0];
min1=array[1];
}
for (i=2; i<size; i++){
t=array[i];
if(t<=min1) {
min2=min1;
min1=t;
continue;
}
if(t<=min2) {
min2=t;
}
}
printf("\nMin elements are 1: %d 2: %d", min1,min2);
printf("\n Absolute difference between two minimum elements are %d\n\n",
abs(min1-min2));
}
int main()
{
//int array[10]={ 9,2,3,4,1,7,5,6,8,0};
//
//int array[10]={3,3,1,33,88,9098,112,33455,678,1};
//int array[10]={5,3,1,33,88,9098,112,33455,678,1};
//int array[10]={2,3,21,33,88,9098,112,33455,678,12};
int array[10]={3,2,21,33,88,9098,112,33455,678,12};
find_two_mins(array,10);
return 0;
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.