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

冒泡排序(Java)

时间:2017-05-12 00:27:36      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:color   out   auto   method   eth   log   范围   public   java   

先看代码

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = {9,5,4,3,7,3,2,1,2};//数据范围是0-10
        for(int i = a.length-1;i>0;i--){
            for(int j=0;j<i;j++){
                if(a[j]>a[j+1]){
                    int temp = 0;
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        for(int i = 0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}

还有一个方法

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = {9,5,4,3,7,3,2,1,2};//数据范围是0-10
        int flag;
        for(int i = a.length-1;i>0;i--){
            flag = 0;
            for(int j=0;j<i;j++){
                if(a[j]>a[j+1]){
                    int temp = 0;
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                    flag = 1;
                }
            }
            if(flag==0){
                break;
            }
        }
        for(int i = 0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}

 

冒泡排序(Java)

标签:color   out   auto   method   eth   log   范围   public   java   

原文地址:http://www.cnblogs.com/LoganChen/p/6842824.html

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