交换排序之冒泡排序

2018-02-26  本文已影响3人  官先生Y

基本思想

冒泡排序是一种交换排序,它的基本思想是:两两比较相邻记录的关键字,如果反序则交换,直到没有反序的记录为止。

两种操作:比较和交换

Java代码实现

冒泡排序非标准版

public class BubbleSortForNonstandard {

    public static void main(String[] args) {

        int[] sequence = new int[]{9, 1, 5, 8, 3, 7, 4, 6, 2};
        System.out.println("排序前:" + Arrays.toString(sequence));
        bubbleSort(sequence);
        System.out.println("排序后:" + Arrays.toString(sequence));
    }

    private static void bubbleSort(int[] sequence) {
        for (int i = 0; i < sequence.length - 1; i++) {
            for (int j = i + 1; j <= sequence.length - 1; j++) {
                if (sequence[i] > sequence[j]) {
                    swap(sequence, i, j);
                }
            }
        }
    }

    private static void swap(int[] sequence, int i, int j) {
        int temp;
        temp = sequence[i];
        sequence[i] = sequence[j];
        sequence[j] = temp;
    }

}

冒泡排序标准版

public class BubbleSortStandard {

    /**
     * 从后往前冒泡,头部元素先确定顺序
     *
     * @param sequence
     */
    public static void bubbleSort(int[] sequence) {
        int length = sequence.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = length - 1; j > i; j--) {
                if (sequence[j - 1] > sequence[j]) {
                    swap(sequence, j - 1, j);
                }
            }
        }
    }

    /**
     * 从前往后冒泡,尾部元素先确定顺序
     *
     * @param sequence
     */
    private static void bubbleSort2(int[] sequence) {
        for (int i = 0; i < sequence.length - 1; i++) {
            for (int j = 0; j < sequence.length - i - 1; j++) {

                if (sequence[j] > sequence[j + 1]) {
                    swap(sequence, j, j + 1);
                }

            }
        }
    }

    private static void swap(int[] sequence, int formerIndex, int latterIndex) {
        int temp;
        temp = sequence[formerIndex];
        sequence[formerIndex] = sequence[latterIndex];
        sequence[latterIndex] = temp;
    }

    public static void main(String[] args) {
        System.out.println("方法一 :从后往前冒泡,头部元素先确定顺序");
        int[] sequence = new int[]{9, 1, 5, 8, 3, 7, 4, 6, 2};
        System.out.println("排序前:" + Arrays.toString(sequence));
        bubbleSort(sequence);
        System.out.println("排序后:" + Arrays.toString(sequence));

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

        System.out.println("方法二 :从前往后冒泡,尾部元素先确定顺序");
        int[] sequence2 = new int[]{9, 1, 5, 8, 3, 7, 4, 6, 2};
        System.out.println("排序前:" + Arrays.toString(sequence2));
        bubbleSort2(sequence2);
        System.out.println("排序后:" + Arrays.toString(sequence2));

    }

}

关注点:

冒泡排序优化版

public class BubbleSortOptimized {

    /**
     * 冒泡排序优化 第一种写法 推荐
     */
    public static void main(String[] args) {
        System.out.println("方法一 :从后往前冒泡,头部元素先确定顺序");
        int[] sequence = new int[]{9, 1, 5, 8, 3, 7, 4, 6, 2};
        sequence = new int[]{2, 1, 3, 4, 5, 6, 7, 8, 9};
        System.out.println("排序前:" + Arrays.toString(sequence));
        bubbleSort(sequence);
        System.out.println("排序后:" + Arrays.toString(sequence));

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

        System.out.println("方法二 :从前往后冒泡,尾部元素先确定顺序");
        int[] sequence2 = new int[]{9, 1, 5, 8, 3, 7, 4, 6, 2};
        sequence2 = new int[]{2, 1, 3, 4, 5, 6, 7, 8, 9};
        System.out.println("排序前:" + Arrays.toString(sequence2));
        bubbleSort2(sequence2);
        System.out.println("排序后:" + Arrays.toString(sequence2));

    }

    /**
     * 冒泡排序优化 第二种写法
     */
    private static void bubbleSort2(int[] sequence) {
        for (int i = 0; i < sequence.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < sequence.length - i - 1; j++) {
                if (sequence[j] > sequence[j + 1]) {
                    swap(sequence, j, j + 1);
                    flag = false;
                }
            }
            if (flag)
                break;
        }
    }

    /**
     * 从后往前冒泡,头部元素先确定顺序
     */
    public static void bubbleSort(int[] sequence) {
        int length = sequence.length;
        boolean flag = true;
        for (int i = 0; i < length - 1 && flag; i++) {
            flag = true;
            for (int j = length - 1; j > i; j--) {
                if (sequence[j - 1] > sequence[j]) {
                    swap(sequence, j - 1, j);
                    flag = false;
                }
            }
        }
    }

    private static void swap(int[] sequence, int formerIndex, int latterIndex) {
        int temp;
        temp = sequence[formerIndex];
        sequence[formerIndex] = sequence[latterIndex];
        sequence[latterIndex] = temp;
    }

}

关键点:

上一篇 下一篇

猜你喜欢

热点阅读