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

java冒泡排序

时间:2015-03-11 12:50:36      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

public class BubbleSort {
    public static void main(String[] args) {
        int score[] = {1,4,5,7,2,3,9,0,6,8};
        for(int i=1;i<score.length;i++){         //最多做n-1趟排序
            for(int j=0;j<score.length-i;j++){   //每次排序范围减少
                if(score[j]<score[j+1]){
                    //交行2个变量数据的3种方法
//                    int temp = score[j];
//                    score[j] = score[j+1];
//                    score[j+1] = temp;
                    
                    
                    score[j] = score[j]^score[j+1];
                    score[j+1] = score[j]^score[j+1];
                    score[j] = score[j]^score[j+1];
                    
//                    score[j] = score[j]+score[j+1];
//                    score[j+1] = score[j]-score[j+1];
//                    score[j] = score[j]-score[j+1];
                }
            }
            System.out.print("第" + i + "次排序结果:");
            for(int a=0;a<score.length;a++){
                System.out.print(score[a]+"\t");
            }
            System.out.println("");
        }
        System.out.print("最终0排序结果:");
        for(int a=0;a<score.length;a++){
            System.out.print(score[a]+"\t");
        }
    }
}

 

java冒泡排序

标签:

原文地址:http://www.cnblogs.com/Nbge/p/4329302.html

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