Java Program To Find Out Second Highest Element In An Array
--------------------------------------------------------------------------------------------------------------------------------
public class SecondHighestEle {
// Java Program To Find Out Second Highest Element In An Array.
public static void main(String[] args) {
int arr[] = { 60, 50, 40, 20, 70 }; // Defining, Declaring and Assigning Array In One Place
int temp = 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]; // Swap the Array elements
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Array is sorted in ascending order
// Print the second highest element from an array
System.out.println("The Second Highest Element Of An Array:" + arr[arr.length-2]);
}
}
Comments
Post a Comment