java,冒泡排序法代码

package com.hanqi;

public class Test15 {

    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 @ 2016-02-29 16:49  什么玩楞啊,我叫旺仔  阅读(4785)  评论(0编辑  收藏  举报