Question: I am trying to get the second smallest using two different functions. But for some reason the second function prints out 2. But when I

I am trying to get the second smallest using two different functions. But for some reason the second function prints out 2. But when I comment the first function call out it will print out 3. Can anyone help explain why this is happening?

public static void main (String args[]) {

int [] input = {3,2,5,6};

System.out.println(minNum1(input));

System.out.println(minNum2(input));

}

public static int minNum1(int [] arr) {

int temp = 0;

int secondMin = 0;

for(int i = 0; i < arr.length; i++) {

for(int j = i + 1; j < arr.length; j++) {

if(arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

secondMin = arr[1];

}

}

return secondMin;

}

public static int minNum2(int [] arr) {

int min = arr[0];

int min2 = arr[1];

for(int i = 0; i < arr.length; i++) {

if(arr[i] < min) {

min2 = min;

min = arr[i];

}

else if(arr[i] < min2) {

min2 = arr[i];

}

}

return min2;

}

please run the code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!