A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈熙 中级黑马   /  2015-8-14 22:07  /  456 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class BubbleSortDemo {
       
        public static void main(String[] args) {
               
                // 创建一个数组
                int[] arr = { 24, 69, 80, 57, 13 } ;
                System.out.print("排序前: ");
                print(arr);
               
                // 排序
                bubbleSort2(arr);
               
                System.out.print("排序后: ");
                print(arr);
               
        }
       
        /**
         * 冒泡排序
         * @param arr
         */
        public static void bubbleSort2(int[] arr){
               
                for(int y = 0 ; y < arr.length - 1 ; y++){
                        for(int x = 0 ; x < arr.length - 1 - y; x++){
                                if(arr[x + 1] < arr[x]){
                                        int temp = arr[x] ;
                                        arr[x] = arr[x + 1];
                                        arr[x + 1 ] = temp ;
                                }
                        }
                }
               
        }
       
        /**
         * 冒泡排序推导过程
         * @param arr
         */
        public static void bubbleSort1(int[] arr){
               
                // 第一次排序
                // arr.length - 1: -1的目的就是防止数组角标越界
                for(int x = 0 ; x < arr.length - 1 - 0; x++){
                        if(arr[x + 1] < arr[x]){
                                int temp = arr[x] ;
                                arr[x] = arr[x + 1];
                                arr[x + 1 ] = temp ;
                        }
                }
               
                // 第二次
                for(int x = 0 ; x < arr.length - 1 - 1; x++){
                        if(arr[x + 1] < arr[x]){
                                int temp = arr[x] ;
                                arr[x] = arr[x + 1];
                                arr[x + 1 ] = temp ;
                        }
                }
               
                // 第三次
                for(int x = 0 ; x < arr.length - 1 - 2; x++){
                        if(arr[x + 1] < arr[x]){
                                int temp = arr[x] ;
                                arr[x] = arr[x + 1];
                                arr[x + 1 ] = temp ;
                        }
                }
               
                // 第四次
                for(int x = 0 ; x < arr.length - 1 - 3; x++){
                        if(arr[x + 1] < arr[x]){
                                int temp = arr[x] ;
                                arr[x] = arr[x + 1];
                                arr[x + 1 ] = temp ;
                        }
                }
               
        }
       
        /**
         * 遍历
         * @param arr
         */
        public static void print(int[] arr){
                System.out.print("[");
                for(int x = 0 ; x < arr.length ; x++){
                        if(x == arr.length - 1){
                                System.out.println(arr[x] + "]");
                        }else {
                                System.out.print(arr[x] + ", ");
                        }
                }
        }

}

6 个回复

倒序浏览
冒泡很有用,搞不好笔试会考。
回复 使用道具 举报
学习了。
回复 使用道具 举报
很棒的总结!
回复 使用道具 举报
  ==     和  != 的意思一样?
回复 使用道具 举报
五月天的倔强 来自手机 初级黑马 2015-10-16 07:45:02
地板
老毕视频里说:冒泡排序的方法要记住,面试和笔试会考;好像说开发时用的不多?而是其他比较容易想的方法?
回复 使用道具 举报
学习学习!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马