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

冒泡排序

时间:2015-11-04 12:56:03      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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 + " ");
        }
    }
}

 

冒泡排序

标签:

原文地址:http://www.cnblogs.com/duanxingxing/p/4935362.html

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