java常用算法之交换排序---冒泡排序

代码块:

public class MaoPao implements SuanFa
{
    private static Logger logger = Logger.getLogger(MaoPao.class);
    @Override
    //方法解析,冒泡的意思就是,
    //第一次把最大的往末端移动
    //第二次把第二大的往末端倒数第二移动,依次类推。
    //相当于把临近两位比较,大的数往后放,然后再拿大的数和后面一位比较大小,最终末尾的将是最大的数。
    public void suanFaRealise(int data[])
    {
        logger.info("maopao realise start.");
        int dataSize = data.length;
        for (int i = 0; i < dataSize - 1; i++)
        {
            logger.debug("1.此时结果为: " + UtilTool.toString(data));
            for (int j = 0; j < dataSize - i - 1; j++)
            {
                if (data[j] > data[j + 1])
                {
                    //交换位置,下一次循环进来data[j]就变成比较大的那个数。
                    int temp = data[j + 1];
                    data[j + 1] = data[j];
                    data[j] = temp;
                    logger.debug("2.此时结果为: " + UtilTool.toString(data));

                }
                else
                {
                    //不交换位置,下次循环进来data[j]就变成较大那个数
                    logger.debug("3.此时结果为: " + UtilTool.toString(data));
                    continue;

                }
            }
        }
    }
}

测试类

public class Test {
    private static Logger logger = Logger.getLogger(Test.class);

    public static void main(String args[])
    {
        int[] test = {6, 1, 22, 7, 9, 33, 98, 55, 23, 78, 48};
        logger.info("maopao test start.");
        SuanFa maopao = SuanFaFactory.getMaoPao();
        maopao.suanFaRealise(test);
        logger.debug(UtilTool.toString(test));
    }
}

执行过程:
[DEBUG] 2020-04-10 13:21:16,713 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [6, 1, 22, 7, 9, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,713 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 22, 7, 9, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,713 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 22, 7, 9, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,715 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 22, 9, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,715 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,715 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,715 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 98, 55, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,715 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 55, 98, 23, 78, 48]
[DEBUG] 2020-04-10 13:21:16,716 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 98, 78, 48]
[DEBUG] 2020-04-10 13:21:16,716 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 98, 48]
[DEBUG] 2020-04-10 13:21:16,716 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,716 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,716 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,717 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 55, 23, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,718 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 78, 48, 98]
[DEBUG] 2020-04-10 13:21:16,719 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,720 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,720 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,720 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 33, 23, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 23, 33, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 55, 48, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:31)
2.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,721 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,722 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,723 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,724 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,724 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,724 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,724 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,724 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,725 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,726 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,727 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:22)
1.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]
[DEBUG] 2020-04-10 13:21:16,727 method:main.java.suanfa.MaoPao.suanFaRealise(MaoPao.java:37)
3.此时结果为: [1, 6, 7, 9, 22, 23, 33, 48, 55, 78, 98]

你可能感兴趣的:(交换排序,算法)