Tuesday 25 September 2012

Java Beginner : Strings and Arrays - Display second largest element in the array of names


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication6;

/**
 *
 * @author Sunshine
 */
public class JavaApplication6 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         String[] names = new String[]{"ravi kiran", "aarushi madiratta", "himani jain", "nancy agarwal","manish kapoor","poonam yadav","silko malik"};
        bubblesort(names);
      
       System.out.println("The second largest element in the arraay of names is : "+names[names.length-2].toUpperCase());
      


    }

    public static void bubblesort(String[] text) {
        int j;
        boolean flag = true;
        String temp;

        while (flag) {
            flag = false;
        
        for (int i = 0; i < text.length - 1; i++) {
            if (text[i].compareToIgnoreCase(text[i + 1]) > 0) {
                temp = text[i];
                text[i] = text[i + 1];
                text[i + 1] = temp;
                flag = true;
            }
        }
    }
    }
}
    



No comments:

Post a Comment