冒泡排序算法(四)

一、冒泡排序优化二
二、算法演示

三、代码算法分析
class MaoPaoSort {
    void maoPaoSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    void maoPaoSortOptimized(int[] array) {

        int count = 0;
        int comparisons = 0;
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            count++;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flag = true;  //说明发生了交换,那么此时将标志位flag设置位true说明发生了交换
                }
                comparisons++;
            }
            if (!flag)   //在每一趟(也就是第一层循环的每一次循环)都判断flag的值,如果flag为false,也就是说这一趟数据无任何交换,那么就说明数据已经被排好了,不需要再进行循环了
                break;
        }
        System.out.println("共走了" + count + "趟");
        System.out.println("共比较了"+comparisons+"次");
    }

    void maoPaoSortOptimizedSecond(int[] array) {
        int count = 0;
        int pos = array.length - 1;
        int comparisons = 0;
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            int record = 0 ;
            //最后一次交换的位置
            count++;
            for (int j = 0; j < pos; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flag = true;  //说明发生了交换,那么此时将标志位flag设置位true说明发生了交换
                    record = j;  //记录最后一次发生交换的位置
                }
                comparisons++;
            }
            if (!flag)   //在每一趟(也就是第一层循环的每一次循环)都判断flag的值,如果flag为false,也就是说这一趟数据无任何交换,那么就说明数据已经被排好了,不需要再进行循环了
                break;
            pos = record;

        }
        System.out.println("共走了" + count + "趟");
        System.out.println("共比较了"+comparisons+"次");
    }

}




import java.util.Arrays;

public class Sort {
    public static void main(String[] args) {
        int[] ints = {2, 34, 4, 55, 88};
        MaoPaoSort maoPaoSort = new MaoPaoSort();
        maoPaoSort.maoPaoSort(ints);
        System.out.println("简单冒泡排序" + Arrays.toString(ints));
        System.out.println("=======================================");

        int[] ints1 = {88, 2, 4, 34, 55};
        maoPaoSort.maoPaoSortOptimized(ints1);
        System.out.println("优化冒泡排序一" + Arrays.toString(ints1));
        System.out.println("=======================================");


        int[] ints2 = {88, 2, 4, 34, 55};
        maoPaoSort.maoPaoSortOptimizedSecond(ints2);
        System.out.println("优化冒泡排序二" + Arrays.toString(ints2));
        System.out.println("=======================================");


        int[] ints3 = {1, 2, 5, 7, 4, 3, 6, 8, 9, 10};
        maoPaoSort.maoPaoSortOptimized(ints3);
        System.out.println("优化冒泡排序一" + Arrays.toString(ints3));

        System.out.println("=======================================");

        int[] ints4 = {1, 2, 5, 7, 4, 3, 6, 8, 9, 10};
        maoPaoSort.maoPaoSortOptimizedSecond(ints4);
        System.out.println("优化冒泡排序二" + Arrays.toString(ints4));
    }
}

四、运行结果对比
简单冒泡排序[2, 4, 34, 55, 88]
=======================================
共走了2趟
共比较了7次
优化冒泡排序一[2, 4, 34, 55, 88]
=======================================
共走了2趟
共比较了7次
优化冒泡排序二[2, 4, 34, 55, 88]
=======================================
共走了4趟
共比较了30次
优化冒泡排序一[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=======================================
共走了4趟
共比较了19次
优化冒泡排序二[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Process finished with exit code 0
posted @ 2020-08-15 20:28  ~清风配酒  阅读(96)  评论(0编辑  收藏  举报