Search This Blog

Showing posts with label Bubble sort. Show all posts
Showing posts with label Bubble sort. Show all posts

Thursday, December 16, 2010

Sorting of city names in alphabetical order using the Bubble Sort technique - ICSE BOARD QUESTION 2008

/**
 * Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order
 * using the Bubble Sort technique only.
 * INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
 * OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai
 */
import java.io.*;
class question2008
{
    public static void main(String args[]) throws IOException
    {
        String words[]= {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"};
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));       
       
        int len=words.length;
        int i,j;
        String t;
       
        for(i=0; i<len-1; i++)
        {
            for(j=0; j<len-1-i; j++)
            {
                if(words[j].compareTo(words[j+1])>0)
                {
                    t=words[j+1];
                    words[j+1]=words[j];
                    words[j]=t;
                }
            }
        }
        System.out.println("The cities in alphabetical order are : ");
        for(i=0; i<len; i++)
        {
            System.out.print(words[i]+ " ");
        }
    }
}