数组冒泡算法

 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 }

转载于:https://www.cnblogs.com/coolso/p/5530195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值