Friday, 30 October 2015

Sorting String


                                                       Sorting String


package com.talent;

public class SortString {

public static void main(String[] args) {

   String []names={"Amit","neeraj","dhiraj","SHIPRA","azak","axdf"};
 
     sortStringBubble(names);
   
     for(int k=0;k<6;k++)
     {
   
    System.out.println(names[k]);
   
   
     }

}

private static void sortStringBubble(String[] x) {


boolean flag=true;

String temp;

while (flag) {


flag=false;

for (int j = 0; j < x.length-1; j++) {

if(x[j].compareToIgnoreCase(x[j+1])>0)

{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
flag=true;

}
}

}


}

}
OutPut->
Sorted String
Amit
axdf
azak
dhiraj
neeraj
SHIPRA

No comments:

Post a Comment