冒泡排序

1.冒泡排序算法,增加flag标记

/**
 * Created by xingxing.duan on 2015/11/4.
 */
public class BubbleSort {

    private static void bubbleSort(int[] matrix) {

        boolean flag = true;
        for (int i = 0; i < matrix.length && flag; i++) {
            flag = false;
            for (int j = 1; j < matrix.length - i; j++) {
                if (matrix[j - 1] > matrix[j]) {
                    int tmp = matrix[j - 1];
                    matrix[j - 1] = matrix[j];
                    matrix[j] = tmp;
                    flag = true;
                }
            }
        }
    }


    public static void main(String[] args) {
        int[] matrix = new int[]{3, 4, 8, 7, 6, 10, 9,9,9,10,10,4};
        bubbleSort(matrix);
        for (int i : matrix) {
            System.out.print(i + " ");
        }
    }
}
原文地址:https://www.cnblogs.com/duanxingxing/p/4935362.html