/**
     * 冒泡排序
     */
    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 + " ");
    }
posted on 2022-05-04 12:15  upupup-999  阅读(35)  评论(0)    收藏  举报