Saturday, 24 September 2016

Java Program to sort Names in an alphabetical order.

Java program to sort Names in an alphabetical order
    Example:  input- 4
                    happy lucky abhay sibu
              output-abhay happy lucky sibu
 
    Example 2: input- 4 
                     arjun hapi babai lucky
               output-arjun babai hapi lucky 

Answer :

 public class SortNames
 {

  public static void main(String[] args) 
  {
        Scanner sc=new Scanner(System.in);
        System.out.println();
        
        int num=sc.nextInt();// how many names you want to sort 

        String[] s=new String[num];

        for(int i=0;i<(num);i++)
        {
           s[i]=sc.next();
        }

       int len=s.length;

       for(int i=0;i<(len);i++)
       {
          String temp;
          for(int j=0;j0)
              {
                  temp=s[j];
                  s[j]=s[j+1];
                  s[j+1]=temp;
              }
          }
       }

       for(String m:s)
       {
          System.out.print(m+" ");
       }
  }

 }

No comments:

Post a Comment