Saturday, May 17, 2008

Finding Second Highest of an Integer Array

Click the above image to enlarge it. This contains a Java program which computes the second highest of an integer array for all possible combination of elements. I've skipped the basic checks to simplify the code.

Algorithm used in the above program
  • Assign the highest and sec_highest both variables as the 0th element of the array
  • Use a loop to go through the entire array and store the current element as 'temp'
  • If the current element is greater than highest then set highest to the current element and the sec_highest to the previous highest
  • Else if current element is only greater than the sec_highest OR if sec_highest is equal to the highest then set sec_highest to the current element
  • Complete the loop and you get second highest element as sec_highest


No comments: