1 public class ArrayDemo06 2 { 3 public static void main(String[] args) 4 { 5 int score[]={67,89,87,69,90,100,75,90}; 6 for (int i=1;i<score.length ;i++ ) 7 { 8 for (int j=0;j<score.length ;j++ )//内层循环是和每一个数比较 9 { 10 if (score[i]<score[j]) 11 { 12 int temp=score[i]; 13 score[i]=score[j]; 14 score[j]=temp; 15 } 16 } 17 } 18 for (int i=0;i<score.length ;i++ ) 19 { 20 System.out.println("从小到大输出:"+score[i]); 21 } 22 } 23 }
详细是输出每一次比较的结果
1 public class ArrayDemo07 2 { 3 public static void main(String[] args) 4 { 5 int score[]={67,89,87,69,90,100,75,90}; 6 for (int i=1;i<score.length ;i++ ) 7 { 8 for (int j=0;j<score.length ;j++ ) 9 { 10 if (score[i]<score[j])//比较后交换两个数 11 { 12 int temp=score[i]; 13 score[i]=score[j]; 14 score[j]=temp; 15 } 16 } 17 System.out.print("第"+i+"次的排序的结果: "); 18 for (int j=0;j<score.length ;j++ ) 19 { 20 System.out.print(score[j]+"\t"); 21 } 22 System.out.println(""); 23 } 24 25 System.out.print("最终的排序结果为 : "); 26 for (int i=0;i<score.length ;i++ ) 27 { 28 System.out.print(score[i]+"\t"); 29 } 30 } 31 }