Find Second Largest Element in an Array

public class SecondLargestInteger {

/**
* @param args
*/
public static void main(String[] args) {

largest();
}

public static void largest() {
int a[] = { 10, 20, 8, 13, 90, 6, 78, 80, 11, 19 };
int high = 0, shigh = 0;
int mid = (a.length) / 2;
for (int i = 0; i < mid; i++) {
if (a[i] > a[i + 1])
high = a[i];

}
for (int j = mid; j < (a.length); j++) {

if (a[mid] > a[mid + 1])
shigh = a[mid];
else
mid++;

}

if (high < shigh)
System.out.println(“Second largest element is” + high);
else
System.out.println(“Second largest element is” + shigh);

}
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s