冒泡排序

本文介绍了冒泡排序的四种优化方法,包括简单的冒泡排序、添加交换标志的优化、记录交换位置的优化以及正向反向扫描的优化。这些优化针对不同场景提高了排序效率,减少了不必要的比较和交换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:
假设两个数a和b,将a异或b的值记做temp,即:
temp=a^b;
那么temp^ a的值为b,并且temp^b的值为a

package sort;

import java.util.Arrays;

/**
 * @author Zhujintao 2021/2/5 13:27
 * @desc BubbleSort.
 */
public class BubbleSort {
    /**
     * 简单冒泡排序
     * @param arr
     */
    public static void bubbleSort1(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j] ^ arr[j + 1];
                    arr[j + 1] = temp ^ arr[j + 1];
                    arr[j] = arr[j] ^ temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 优化一:添加一个标志,当数组循环遍历时某次循环不再交换元素位置表示排序完成,避免了固定循环次数导致的数组排序完成仍然循环遍历的问题
     * 适用于少量数据乱序的数组.
     *
     * @param arr
     */
    public static void bubbleSort2(int[] arr) {
        for (int i = 0; i < arr.length - 1 ; i++) {
            boolean isExchange = false;
            for (int j = 0; j < arr.length - i - 1 ; j++) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j] ^ arr[j + 1];
                    arr[j + 1] = temp ^ arr[j +1];
                    arr[j] = temp ^ arr[j];
                    isExchange = true;
                }
            }
            if (!isExchange) {break;}
        }
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 优化二:每次记录交换位置的元素下标,下次只需遍历到此即可,因为后面的已经是有序了。
     * 适用于前面大部分无序,后面大部分有序的数组.
     *
     * @param arr
     */
    public static void bubbleSort3(int[] arr) {
        int index = 0;
        int len = arr.length - 1;
        for (int i = 0; i < arr.length - 1; i++) {
            boolean isExchenge = false;
            for (int j = 0; j < len; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j] ^ arr[j + 1];
                    arr[j + 1] = temp ^ arr[j + 1];
                    arr[j] = temp ^ arr[j];
                    index = j;
                    isExchenge = true;
                }
            }
            len = index;
            if (!isExchenge){break;}
        }
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 优化三:正向扫描找到最大值交换到最后,反向扫描找到最小值交换到最前面。
     *
     * @param arr
     */
    public static void bubbleSort4(int[] arr) {
        int maxIndex = 0;
        int minIndex = 0;
        for (int i = 0; i < arr.length - 1 ; i++) {
            boolean isExchenge = false;
            for (int j = 0; j < arr.length - 1 ; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j] ^ arr[j + 1];
                    arr[j + 1] = temp ^ arr[j + 1];
                    arr[j] = temp ^ arr[j];
                    maxIndex = j;
                    isExchenge = true;
                }
            }
            for (int m = maxIndex; m > minIndex ; m--) {
                if (arr[m] < arr[m - 1]) {
                    int temp = arr[m] ^ arr[m - 1];
                    arr[m - 1] = temp ^ arr[m - 1];
                    arr[m] = temp ^ arr[m];
                    minIndex = m;
                    isExchenge = true;
                }
            }
            if (!isExchenge){break;}
        }
        System.out.println(Arrays.toString(arr));
    }
    public static void main(String[] args) {
        int[] arr = {3, 2, 1, 5, 4, 7, 6, 8, 9, 10, 11, 0};
        bubbleSort1(arr);

        int[] arr2 = {3, 2, 1, 5, 4, 7, 6, 8, 9, 10, 11, 0};
        bubbleSort2(arr2);

        int[] arr3 = {3, 2, 1, 5, 4, 7, 6, 8, 9, 10, 11, 0};
        bubbleSort3(arr3);

        int[] arr4 = {3, 2, 1, 5, 4, 7, 6, 8, 9, 10, 11, 0};
        bubbleSort4(arr4);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值