代码如下:
public class ArrayDemo07{
public static void main(String args[]){
int score[] = {3,1,2,5,6} ;
for(int i=1;i<score.length;i++){
for(int j=0;j<score.length;j++){
if(score[i]<score[j]){
int temp = score[i] ;
score[i] = score[j] ;
score[j] = temp ;
}
}
System.out.print("第" + i + "次排序的结果:") ;
for(int j=0;j<score.length;j++){
System.out.print(score[j]+"\t") ;
}
System.out.println("") ;
}
for(int i=0;i<score.length;i++){
System.out.print(score[i]+"\t") ;
}
}
  输出结果如下:
第一次排序结果:1,6,2,3,5
第二次排序结果:1,2,6,3,5
第三次排序结果:1,2,3,6,5
第一次排序结果:1,2,3,5,6
小弟就是不明白为什么6会跑到前面来,不是两个数比较小的放前面吗?各位请各位大侠解答啊

解决方案 »

  1.   

    实际上这样的例子不只排序4次import java.util.Arrays;public class ArrayDemo07 {
    public static void main(String args[]) {
    int score[] = { 3, 1, 2, 5, 6 };
    for (int i = 1; i < score.length; i++) {
    for (int j = 0; j < score.length; j++) {
    if (score[i] < score[j]) {
    int temp = score[i];
    score[i] = score[j];
    score[j] = temp;
    }
    System.out.print("第" + ((i - 1) * 5 + j) + "次排序的结果:");
    System.out.println(Arrays.toString(score));
    }
    }
    for (int i = 0; i < score.length; i++) {
    System.out.print(score[i] + "\t");
    }
    }
    }
      

  2.   


    总共20次比较,i=0,和j比较5次,i=1,和j比较5次.......,以此类退,所以总共是4*5=20次