Tuesday, July 6, 2010

Second Highest Element in Array



package sun.sort;

public class SecondHigestinArray {

public static void main(String[] args) {

int arr[]={1,4,2,9,6,7,8,9};

int temp,h,sH;
sH=h=arr[0];

for (int i = 0; i < arr.length; i++) {

temp=arr[i];
if (temp > h) {

sH=h;
h=temp;
}else if(temp > sH || sH==h){

sH=temp;
}
}
System.out.println("second Highest "+sH);