码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡法数组排序

时间:2018-08-18 13:13:04      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:void   ali   for   system   ima   java   system.in   scanner   new   

用冒泡法给数组从小到大和从小到大排序

 

import java.util.Scanner;        
public class six4{
    public static void main(String []args){
        int score[]=new int [10];
        Scanner s=new Scanner(System.in);
        for(int i=0;i<score.length;i++)
            score[i]=s.nextInt();        
        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;
            }
        for(int i=0;i<score.length;i++)        //按从小到大的顺序输出
            System.out.print(score[i]+"\t"); 
        for(int i=score.length-1;i>=0;i--)    //按从大到小的顺序输出
            System.out.print(score[i]+"\t");
    }
}

 运行结果如下:

技术分享图片

 

冒泡法数组排序

标签:void   ali   for   system   ima   java   system.in   scanner   new   

原文地址:https://www.cnblogs.com/l666/p/9496553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!