package utils;
public class StringSort {
public static void main(String[] args) {
String[] a=new String[5];
a[0]="D";
a[1]="E";
a[2]="C";
a[3]="B";
a[4]="A";
for(String o: a){
System.out.println("before sort: "+o);
}
System.out.println("################################");
sort(a);
for(String o: a){
System.out.println("affter sort: "+o);
}
}
// Sort a String array using selection sort.
static String[] sort(String[] a) {
for (int i=0; iTag: Study Code Program Java
for (int j=i+1; j
if (a[i].compareTo(a[j]) > 0) {
String temp=a[j]; a[j]=a[i]; a[i]=temp;
}
}
}
return a;
}
}
/**
* ##### OUTPUT ######
before sort: D
before sort: E
before sort: C
before sort: B
before sort: A
################################
#affter sort: A
#affter sort: B
#affter sort: C
#affter sort: D
#affter sort: E
*/
Sunday, December 19, 2010
String Sort
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment