String[] arr = { "Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
"to", "come", "to", "the", "aid", "of", "their", "country"
};
public class Sort {
public static void main(String[] args) {
String[] arr = { "Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
String temp;
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr.length-1-i; j++) {
if(arr[j].compareTo(arr[j+1]) > 0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
import java.util.Arrays;public class TestStringBubble {
public static void main(String[] args){
String[] arr = { "Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
Arrays.sort(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
import java.util.Random;
public class MaoPaoSort { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []arg=new int[5];
Random r=new Random();
for(int k=0;k<arg.length;k++)
arg[k]=r.nextInt();
int[]a1=arg,a2=arg,a3=arg;
System.out.println("非排序的数组:");
for(int i=0;i<arg.length;i++)
System.out.print(arg[i]+" ");
Asc1(arg);
System.out.println("\n升序排序后的数组:");
for(int i=0;i<arg.length;i++)
System.out.print(arg[i]+" ");
Asc2(a1);
System.out.println("\n升序排序后的数组:");
for(int i=0;i<a1.length;i++)
System.out.print(a1[i]+" ");
Desc1(a2);
System.out.println("\n降序排序后的数组:");
for(int i=0;i<a2.length;i++)
System.out.print(a2[i]+" ");
Desc2(a3);
System.out.println("\n降序排序后的数组:");
for(int i=0;i<a3.length;i++)
System.out.print(a3[i]+" ");
} static void Asc1(int[] args)//升序排序,从下往上找
{
int counter = 0, counter_ = 0 , tmp =0;
for (counter = 1; counter <= args.length - 1; counter++)
{
for (counter_ = args.length - 1; counter_ >= counter; counter_--)
{
if (args[counter_] < args[counter_ - 1])
{
tmp = args[counter_];
args[counter_] = args[counter_ - 1];
args[counter_ - 1] = tmp;
}//end of if
}//end of nei for
}//end of for
}//end of function
static void Desc1(int[] args)//降序排序,从下往上找
{ int counter = 0, counter_ = 0, tmp = 0;
for (counter = 1; counter <= args.length - 1; counter++)
{
for (counter_ = args.length - 1; counter_ >= counter; counter_--)
{
if (args[counter_] > args[counter_ - 1])
{
tmp = args[counter_];
args[counter_] = args[counter_ - 1];
args[counter_ - 1] = tmp;
}//end of if }//end of nei for
}//end of for
}//end of function
static void Desc2(int[] args)//降序,从上往下找
{
int tmp = 0;
for (int outer = 1; outer <= args.length - 1; outer++)
{
for (int inner = 0; inner <=args.length-1- outer; inner++)
{
if (args[inner] > args[inner +1])
{
tmp = args[inner];
args[inner] = args[inner+1];
args[inner ] = tmp;
}//end of if
}
}//end of for
}//end of function
static void Asc2(int[] args)//升序,从上往下找
{
int tmp = 0;
for (int outer = 1; outer <= args.length - 1; outer++)
{
for (int inner = 0; inner <= args.length - 1 - outer; inner++)
{
if (args[inner] > args[inner + 1])
{
tmp = args[inner];
args[inner] = args[inner+1];
args[inner+1] = tmp;
}//end of if
}
}//end of for
}//end of function
}