再论经典冒泡排序算法

冒泡排序基本原理:

从队列的底部开始,依次与前一个元素相比较,如果条件跟排序条件不符合,就交换。这样一轮比较下来,第一个元数就是最小的元素了。由于过程类似于夏天河底冒出汽泡,汽泡比水轻而上浮,所以叫冒泡排序。

有一种改进的形态,叫下沉法,类似于往河中扔东西,其过程是往下沉的。

以下用到的公用方法:

    /**
     * 是否升序排列,true:升序排列, false:降序排列
     */
    private static boolean isAscending = true;

    /**
     * 描述:判断前面元素跟后面元素的关系是否升序/降序。
     * 
     * @param a 数组
     * @param prev 前面元素
     * @param next 后面元素
     * @param isAscending 是否升序排列,true:升序排列, false: 降序排列
     * @return
     */
    private static boolean judge(long prev, long next, boolean isAscending) {
        return isAscending ? prev < next : prev > next;
    }

    /**
     * 描述:交换元素。
     * 
     * @param a 数组
     * @param i 元素1
     * @param j 元素2
     */
    private static void swap(int[] a, int i, int j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    /**
     * 描述:排序测试。
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        int[] a = { 1, 3, 7, 8, 5, 9, 6, 2, 4, 0 };
        System.out.println("数组长度:" + a.length);
        System.out.println("数组原有顺序:" + Arrays.toString(a));
        SortUtil.bubbleSort(a);
        System.out.println("数组排序后顺序:" + Arrays.toString(a));
    }

1.经典冒泡排序法(上浮法):
    /**
     * 描述:冒泡排序(交换排序)。上浮法。
     * <p>
     * 从未排序队列中,取出队尾元素,与前一元素比较并确定是否交换。重复前面的操作步骤。
     * 
     * @param a 数组
     */
    public static void bubbleSort(int[] a) {
        int loopCount = 0;
        int swapCount = 0;
        for (int i = 0, n = a.length; i < n; i++) {
            // i之前的为已排序的有序队列(严格:已是最终结果的一部分)
            for (int j = n - 1; j > i; j--) {
                if (judge(a[j - 1], a[j], !isAscending)) {
                    swap(a, j, j - 1);
                    swapCount++;
                }
                loopCount++;
            }
        }
        System.out.println("loopCount=" + loopCount);
        System.out.println("swapCount=" + swapCount);
    }

2.形态改变冒泡排序法(下沉法):
    /**
     * 描述:冒泡排序(交换排序)。下沉法。
     * <p>
     * 从未排序队列中,取出队首元素,与后一元素比较并确定是否交换。重复前面的操作步骤。
     * 
     * @param a 数组
     */
    public static void bubbleSort4(int[] a) {
        int loopCount = 0;
        int swapCount = 0;
        for (int i = 0, n = a.length; i < n; i++) {
            for (int j = 0, m = n - i - 1; j < m; j++) {
                if (judge(a[j], a[j + 1], !isAscending)) {
                    swap(a, j, j + 1);
                    swapCount++;
                }
                loopCount++;
            }
        }
        System.out.println("loopCount=" + loopCount);
        System.out.println("swapCount=" + swapCount);
    }

3.经典冒泡法改进(快速返回的):

场景:在基本有序的队列中,可能有很多环节只做了比较,没做交换,如果一整轮比较都未交换过,说明接下来不用再处理了,都是有序了。

    /**
     * 描述:冒泡排序(交换排序)。上浮法。改进型。
     * <p>
     * 从未排序队列中,取出队尾元素,与前一元素比较并确定是否交换。重复前面的操作步骤。
     * 
     * @param a 数组
     */
    public static void bubbleSort2(int[] a) {
        int loopCount = 0;
        int swapCount = 0;
        // 判断处理过程中是否有交换
        boolean isExchanged = true;
        for (int i = 0, n = a.length; i < n && isExchanged; i++) {
            // i之前的为已排序的有序队列(严格:已是最终结果的一部分)
            isExchanged = false;
            for (int j = n - 1; j > i; j--) {
                if (judge(a[j - 1], a[j], !isAscending)) {
                    swap(a, j, j - 1);
                    swapCount++;
                    isExchanged = true;
                }
                loopCount++;
            }
        }
        System.out.println("loopCount=" + loopCount);
        System.out.println("swapCount=" + swapCount);
    }

4.经典冒泡改进(快速返回的,利用上次排序结果):

场景:在基本有序的队列中,可能有很多环节只做了比较,没做交换,如果一整轮比较都未交换过,说明接下来不用再处理了,都是有序了。但如果只做了部分交换,则可以跳过那部分没交换的数据。

    /**
     * 描述:冒泡排序(交换排序)。上浮法。改进型。
     * <p>
     * 从未排序队列中,取出队尾元素,与前一元素比较并确定是否交换。重复前面的操作步骤。
     * 
     * @param a 数组
     */
    public static void bubbleSort3(int[] a) {
        int loopCount = 0;
        int swapCount = 0;
        // 判断处理过程中是否有交换
        boolean isExchanged = true;
        // 上次交换时的索引
        int lastExchangeIndex = 0;
        for (int i = 0, n = a.length; i < n && isExchanged; i++) {
            // i之前的为已排序的有序队列(严格:已是最终结果的一部分)
            isExchanged = false;
            lastExchangeIndex = i;
            for (int j = n - 1; j > i; j--) {
                if (judge(a[j - 1], a[j], !isAscending)) {
                    swap(a, j, j - 1);
                    swapCount++;
                    isExchanged = true;
                    lastExchangeIndex = j -1;
                }
                loopCount++;
            }
            i = lastExchangeIndex;
        }
        System.out.println("loopCount=" + loopCount);
        System.out.println("swapCount=" + swapCount);
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值