其他分享
首页 > 其他分享> > 2.冒泡排序(稳定)

2.冒泡排序(稳定)

作者:互联网

    /**
     * 冒泡排序
     */
    public static void bubbleSort(int[] arr) {
        int length = arr.length;
        for (int i = 1; i < length; i++) {//比较趟数为数据量-1
            for (int j = 0; j < length - i; j++) {//每次比较次数总数据量-躺数,j表示最大到比较次数的前一个
                if (arr[j] > arr[j +1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j +1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }


    //main方法测试
    public static void main(String[] args) {
        int[] b = {49, 38, 65, 97, 76, 13, 27, 50};
        //int[] c = { 13, 38, 65, 97, 76, 13, 2, 50 };//稳定性判断
        bubbleSort(b);
        for (int i : b)
            System.out.print(i + " ");
    }

标签:稳定,arr,int,13,冒泡排序,length,数据量
来源: https://www.cnblogs.com/upupup-999/p/16220784.html