• 排序算法-冒泡排序(工具类)


    冒泡排序

    什么是冒泡排序

    冒泡排序(Bubble Sort),是计算机科学领域简单的排序算法

    重复的访问每一个元素,依次相邻的两个元素进行比较大小,进行交换位置,

    为什么叫冒泡排序:

    • 越小的元素会经过交换位置来浮到数组的顶端,(升序或降序的排序),类似水中气泡慢慢浮现到水面

    原理

    冒泡排序的原理是通过比较相邻的元素,将较大的元素交换到右侧,较小的元素交换到左侧,从而实现排序的目的。

    具体实现是通过嵌套两个循环,外层循环控制排序趟数,内层循环控制每一趟排序多少次。

    在每一趟排序中,如果前一个数比后一个数大,则交换两个数的位置。

    升序

    @Test
    public void testBubbling() {
        int[] a = {1, 47, 4, 36, 8, 2, 90, 2, 3};
        //将数组长度赋值给变量,(在for循环中每次计算数组长度)
        int aLength = a.length;
        System.out.println("未排序前的数组"+Arrays.toString(a));
    
        //排序次数
        for (int i = 1; i < aLength; i++) {
            //循环索引
            for (int j = 0; j < aLength - i; j++) {
                //比较大小
                if (a[j] > a[j + 1]) {
                    //调换位置
                    int stemp = a[j]; //大值赋值给变量
                    a[j] = a[j + 1];//小值往前赋值
                    a[j + 1] = stemp;//变量值向当前索引赋值
                }
            }
        }
        System.out.println("排序后的结果"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    运行结果

    在这里插入图片描述

    分析

    • 依次对相邻两个元素做比较
      在这里插入图片描述

    降序

    @Test
    public void testBubbling() {
        int[] a = {1, 47, 4, 36, 8, 2, 90, 2, 3};
        //将数组长度赋值给变量,(在for循环中每次计算数组长度)
        int aLength = a.length;
        System.out.println("未排序前的数组"+Arrays.toString(a));
    
        //排序次数
        for (int i = 1; i < aLength; i++) {
            //循环索引
            for (int j = 0; j < aLength - i; j++) {
                //比较大小
                if (a[j] < a[j + 1]) {
                    //调换位置
                    int stemp = a[j+1]; //大值赋值给变量
                    a[j+1] = a[j];//小值往后赋值
                    a[j] = stemp;//变量值向当前索引赋值
                }
            }
        }
        System.out.println("排序后的结果"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    运行结果

    在这里插入图片描述

    冒泡排序工具类

    方法返回类型作用参数说明访问权限
    ascengSort(byte[] a)byte[]对byte数组升序排序传入byte数组公开
    ascengSort(byte[] a,int fromIndex,int toIndex)byte[]对byte数组具体索引升序排序传入byte数组,开始索引,结束索引公开
    ascengSort(short[] a)short[]对short数组升序排序传入short数组公开
    ascengSort(short[] a,int fromIndex,int toIndex)short[]对short数组具体索引升序排序传入short数组,开始索引,结束索引公开
    ascengSort(int[] a)int[]对int数组升序排序传入int数组公开
    ascengSort(int[] a,int fromIndex,int toIndex)int[]对int数组具体索引升序排序传入int数组,开始索引,结束索引公开
    ascengSort(long[] a)long[]对long数组升序排序传入long数组公开
    ascengSort(long[] a,int fromIndex,int toIndex)long[]对long数组具体索引升序排序传入long数组,开始索引,结束索引公开
    ascengSort(double[] a)double[]对double数组升序排序传入double数组公开
    ascengSort(double[] a,int fromIndex,int toIndex)double[]对double数具体索引升序排序传入double数组,开始索引,结束索引公开
    ascengSort(float[] a)float[]对float数组升序排序传入float数组公开
    ascengSort(float[] a,int fromIndex,int toIndex)float[]对float数组具体索引升序排序传入float数组,开始索引,结束索引公开
    ascengSort(char[] a)char[]对char数组升序排序传入char数组公开
    ascengSort(char[] a,int fromIndex,int toIndex)char[]对char数组具体索引升序排序传入char数组,开始索引,结束索引公开
    dropSort(byte[] a)byte[]对byte数组降序排序传入byte数组公开
    dropSort(byte[] a, int fromIndex, int toIndex)byte[]对byte数组具体索引降序排序传入byte数组,开始索引,结束索引公开
    dropSort(short[] a)short[]对short数组降序排序传入short数组公开
    dropSort(short[] a, int fromIndex, int toIndex)short[]对short数组具体索引降序排序传入short数组,开始索引,结束索引公开
    dropSort(int[] a)int[]对int数组降序排序传入int数组公开
    dropSort(int[] a, int fromIndex, int toIndex)int[]对int数组具体索引降序排序传入int数组,开始索引,结束索引公开
    dropSort(long[] a)long[]对long数组降序排序传入long数组公开
    dropSort(long[] a, int fromIndex, int toIndex)long[]对long数组具体索引降序排序传入long数组,开始索引,结束索引公开
    dropSort(double[] a)double[]对double数组降序排序传入double数组公开
    dropSort(double[] a, int fromIndex, int toIndex)double[]对double数组具体索引降序排序传入double数组,开始索引,结束索引公开
    dropSort(float[] a)float[]对float数组降序排序传入float数组公开
    dropSort(float[] a, int fromIndex, int toIndex)float[]对float数组具体索引降序排序传入float数组,开始索引,结束索引公开
    dropSort(char[] a)char[]对char数组降序排序传入char数组公开
    dropSort(char[] a, int fromIndex, int toIndex)char[]对char数组具体索引降序排序传入char数组,开始索引,结束索引公开
    rangeCheck(int arrayLength, int fromIndex, int toIndex)void索引范围比较数组长度,开始索引,结束索引私有
    package com.sin.demo.utli;
    
    /**
     * @author sin
     * @date 2022/10/31
     * @apiNote
     */
    public class BubbleSortUtlis {
    
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public byte[] ascengSort(byte[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        byte cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public byte[] ascengSort(byte[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        byte cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public short[] ascengSort(short[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        short cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public short[] ascengSort(short[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        short cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public int[] ascengSort(int[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        int cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public int[] ascengSort(int[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        int cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public long[] ascengSort(long[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        long cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public long[] ascengSort(long[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        long cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public double[] ascengSort(double[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        double cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public double[] ascengSort(double[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        double cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public float[] ascengSort(float[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        float cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public float[] ascengSort(float[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        float cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 升序排序
         *
         * @param a 传入的数组
         * @return
         */
        public char[] ascengSort(char[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        char cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 升序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public char[] ascengSort(char[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] > a[j + 1]) {
                        char cache = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = cache;
                    }
                }
            }
            return a;
        }
    
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public byte[] dropSort(byte[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        byte cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public byte[] dropSort(byte[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        byte cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public short[] dropSort(short[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        short cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public short[] dropSort(short[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        short cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public int[] dropSort(int[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        int cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public int[] dropSort(int[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        int cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public long[] dropSort(long[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        long cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public long[] dropSort(long[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        long cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public double[] dropSort(double[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        double cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public double[] dropSort(double[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        double cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
    
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public float[] dropSort(float[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        float cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public float[] dropSort(float[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        float cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
    
        /**
         * 降序排序
         *
         * @param a 传入的数组
         * @return
         */
        public char[] dropSort(char[] a) {
            int aLength = a.length;
            int i, j;
            for (i = 1; i < aLength; i++) {
                for (j = 0; j < aLength - 1; j++) {
                    if (a[j] < a[j + 1]) {
                        char cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
        /**
         * 降序排序
         *
         * @param a         传入的数组
         * @param fromIndex 开始元素
         * @param toIndex   结束元素
         * @return
         */
        public char[] dropSort(char[] a, int fromIndex, int toIndex) {
            int aLength = a.length;
            //对索引范围的校验
            this.rangeCheck(aLength, fromIndex, toIndex);
    
            int cycles = fromIndex - toIndex;
    
            int i, j;
            for (i = cycles; i < aLength; i++) {
                for (j = fromIndex; j < toIndex; j++) {
                    if (a[j] < a[j + 1]) {
                        char cache = a[j + 1];
                        a[j + 1] = a[j];
                        a[j] = cache;
                    }
                }
            }
            return a;
        }
    
        /**
         * 索引范围比较
         *
         * @param arrayLength 数组长度
         * @param fromIndex   开始索引
         * @param toIndex     结束索引
         */
        private void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
            /**
             * 如果开始索引比结束索引大
             * 则排除非法参数异常
             */
            if (fromIndex > toIndex) {
                throw new IllegalArgumentException(
                        "开始索引(" + fromIndex + ") > 结束索引(" + toIndex + ")");
            }
            /**
             * 如果开始索引小于0
             * 则抛出数组索引越界异常
             */
            if (fromIndex < 0) {
                throw new ArrayIndexOutOfBoundsException(fromIndex);
            }
            /**
             * 如果结束索引小于0
             * 则抛出数组索引越界异常
             */
            if (toIndex > arrayLength) {
                throw new ArrayIndexOutOfBoundsException(toIndex);
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728

    BubbleSortUtlis工具类测试

    //实例化工具类
    BubbleSortUtlis bubbleSortUtlis = new BubbleSortUtlis();
    
    • 1
    • 2

    int类型数组升序排序

    @Test
    public void testIntAscengSort() {
        int[] a = {2,5,3,8,1,9,23,11,6};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.ascengSort(a);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    int类型数组对具体元素升序排序

    @Test
    public void testIntAscengSort() {
        int[] a = {2,5,3,8,1,9,23,11,6};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.ascengSort(a,3,7);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    int类型降序排序

    @Test
    public void testIntAscengSort() {
        int[] a = {2,5,3,8,1,9,23,11,6};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.dropSort(a);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    int类型对具体元素降序排序

    @Test
    public void testIntAscengSort() {
        int[] a = {2,5,3,8,1,9,23,11,6};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.dropSort(a,2,5);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    char类型数组升序排序

     @Test
        public void testCharAscengSort() {
            char[] a = {'a', 'v', 'd', 'e', 'f', 'c', 'b'};
            System.out.println("排序前"+Arrays.toString(a));
            bubbleSortUtlis.ascengSort(a);
            System.out.println("排序后"+Arrays.toString(a));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    结束结果
    在这里插入图片描述

    char类型数组对具体元素升序排序

    @Test
    public void testCharAscengSort() {
        char[] a = {'a', 'v', 'd', 'e', 'f', 'c', 'b'};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.ascengSort(a,2,5);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    char类型数组降序排序

    @Test
    public void testCharAscengSort() {
        char[] a = {'a', 'v', 'd', 'e', 'f', 'c', 'b'};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.dropSort(a);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

    char类型数组对具体元素降序排序

    @Test
    public void testCharAscengSort() {
        char[] a = {'a', 'v', 'd', 'e', 'f', 'c', 'b'};
        System.out.println("排序前"+Arrays.toString(a));
        bubbleSortUtlis.dropSort(a,2,4);
        System.out.println("排序后"+Arrays.toString(a));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    测试结果

    在这里插入图片描述

  • 相关阅读:
    腾讯算法实习面试总结
    牛客练习赛105(A切蛋糕的贝贝、B抱歉,这没有集美、D点分治分点)
    高低电平触发,(边沿触发)上升沿触发和下降沿触发 中断区别
    漏刻有时数据可视化Echarts组件开发(26):全国地图三级热力图下钻和对接api自动调用数据开发实录
    Angular 依赖注入介绍及使用(五)
    在vue3框架中使用vant的input组件安卓端闪退的问题
    Fabric.js 上划线、中划线(删除线)、下划线
    从零搭建react + webpack项目
    学习编程-先改变心态
    Flutter Map 常用操作方法概述
  • 原文地址:https://blog.csdn.net/qq_44715376/article/details/127621389