Advertisement
d1i2p3a4k5

12. Sorting of strings

Oct 18th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. class sort
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner t = new Scanner(System.in);
  8.         System.out.println("enter total no. of strings");
  9.         int n = t.nextInt();
  10.         String s[] = new String[n];
  11.         int i,j,k,l;
  12.         System.out.println("enter strings");
  13.         for(i=0;i<n;i++)
  14.         {
  15.             s[i]=t.next();
  16.         }
  17.         String temp = new String();
  18.         for(i=0;i<=n-2;i++)
  19.         {
  20.             for(j=0;j<=n-2;j++)
  21.             {
  22.                 if(s[j].compareTo(s[j+1])>0)
  23.                 {
  24.                     temp = s[j];
  25.                     s[j] = s[j+1];
  26.                     s[j+1] = temp;
  27.                
  28.                 }
  29.             }
  30.         }
  31.         System.out.println("sorted strings are given below:");
  32.         for(i=0;i<n;i++)
  33.         {
  34.             System.out.println(s[i]);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement