Cara mengurutkan data berdasarkan alfabet. Contohnya di baah ini.
Bubble Sort:
public class AlphaSortingBubble
{
public static void main(String[ ] args)
{
String[ ] names = {"joe", "slim", "ed", "george"};
sortStringBubble (names);
for ( int k = 0; k < 4; k++ )
System.out.println( names [ k ] );
}
public static void sortStringBubble( String x [ ] )
{
int j;
boolean flag = true; // will determine when the sort is finished
String temp;
{
public static void main(String[ ] args)
{
String[ ] names = {"joe", "slim", "ed", "george"};
sortStringBubble (names);
for ( int k = 0; k < 4; k++ )
System.out.println( names [ k ] );
}
public static void sortStringBubble( String x [ ] )
{
int j;
boolean flag = true; // will determine when the sort is finished
String temp;
while ( flag )
{
flag = false;
for ( j = 0; j < x.length - 1; j++ )
{
if ( x [ j ].compareToIgnoreCase( x [ j+1 ] ) > 0 )
{ // ascending sort
temp = x [ j ];
x [ j ] = x [ j+1]; // swapping
x [ j+1] = temp;
flag = true;
}
}
}
}
}
{
flag = false;
for ( j = 0; j < x.length - 1; j++ )
{
if ( x [ j ].compareToIgnoreCase( x [ j+1 ] ) > 0 )
{ // ascending sort
temp = x [ j ];
x [ j ] = x [ j+1]; // swapping
x [ j+1] = temp;
flag = true;
}
}
}
}
}
No comments:
Post a Comment