Tuesday 25 September 2012

Java Beginner : Strings and Arrays - Sort Array of names


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

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String[] names = new String[]{"ravi kiran", "arushi madiratta", "himani jain", "nancy agarwal","manish kapoor","poonam yadav","silko malik"};
        bubblesort(names);
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }


    }

    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