那消逝的岁月

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public static void main(String[] args) {


        //冒泡排序 从小到大
        int[] a = new int[]{23,45,67,12,97,78,36,8};
        
        System.out.print("原始顺序:");
        
        for(int t : a)
        {
            System.out.print(t + " ");
        }
        
        System.out.println();
        
        int m = 0;
        
        //循环次数 0-6
        for(int j=0; j<a.length - 1; j++)
        {        
            //前后比较循环 0-6
            for(int i=0; i<a.length - 1 - j; i++)
            {
                //比较前后元素的大小顺序
                if(a[i] > a[i+1])
                {
                    //临时存放
                    int b = a[i];

                    a[i] = a[i+1];

                    a[i+1] = b;
                }
                m++;
            }

            System.out.print((j+1) + "次循环:");

            for(int t : a)
            {
                System.out.print(t + " ");
            }
            
            System.out.println();
        }
        
        System.out.println("m=" + m);
        



    }

posted on 2016-03-10 16:32  那消逝的岁月  阅读(230)  评论(0编辑  收藏  举报