Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Find the 3rd smallest number in a Java array.
Example
Following is the required program.
public class Tester {
public static int getThirdSmallest(int[] a) {
int temp;
//sort the array
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
//return third smallest element
return a[2];
}
public static void main(String args[]) {
int a[] = { 11,10,4, 15, 16, 13, 2 };
System.out.println("Third Smallest: " +getThirdSmallest(a));
}
}
Output
Third smallest: 10
Advertisements
