Menu



Java program for find second largest number in an array


bubble-sort.java

public class SecondLargestInArray{
public static int getSecondLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2,4,8};
int b[]={44,66,99,77,66,88,33,22,55};
System.out.println("Second Largest: "+getSecondLargest(a,8));
System.out.println("Second Largest: "+getSecondLargest(b,9));
}
}