java Bubble sort

1 图形

2 java 冒泡排序

        int[] arr = {5, 4, 3, 2, 1};
        for(int i =0;i<arr.length-1;i++) {//arr.length = 5, i <= 3
            for (int j = 0; j < arr.length-i-1; j++) { // j <= 3
                if (arr[j] > arr[j + 1]) {// 和+1 元素比较,大于互换
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int temp : arr){
            System.out.print(temp + " ");
        }

猜你喜欢

转载自blog.csdn.net/qq_28197211/article/details/80731408
今日推荐