forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondSmallest.java
More file actions
47 lines (36 loc) · 1.17 KB
/
SecondSmallest.java
File metadata and controls
47 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package coding;
import java.io.IOException;
import java.util.Arrays;
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;
public class SecondSmallest {
int[] data = {3,2,11,4,6,7};
@Test(priority=1)
public void secondSmallestUsingArray() throws IOException {
if (data.length < 2) {
System.out.println(" Invalid Input ");
} else {
Arrays.sort(data); // useful it is not sorted
System.out.println(data[1]);
}
}
@Test(priority=2)
public void secondSmallestUsingLoop() throws IOException {
/* There should be at least two elements */
if (data.length < 2) {
System.out.println(" Invalid Input ");
} else {
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int i = 0; i < data.length; i++) {
if (data[i] < smallest) {
secondSmallest = smallest;
smallest = data[i];
} else if (data[i] < secondSmallest) {
secondSmallest = data[i];
}
}
System.out.println("The smallest element is: " + smallest + " and "+ "the second smallest element is: " + secondSmallest);
}
}
}