I want to find the second minimum in a array list.Here's my code.Is there a better way to do it?
int main(){
int a[5]={7,5,45,89,12};
int smallest=a[0];
int index;
for(int i=0;i<5;i++){
if(a[i]<smallest){
smallest=a[i];
index=i;
}
}
smallest=a[0];
for(int i=0;i<5;i++){
cout<<i;
if((a[i]<smallest )&& (i!=index)){
smallest=a[i];
}
}
cout<<"second smallest value is: "<<smallest;
This code runs in O(n) time? For the first loop it takes n steps, and for the other for loop also it takes n steps.Therefore altogether it takes O(n) time complexity .
Is this right?Can someone correct me if I am wrong please