冒泡排序(数组)

public class myBubbleSort {
    public static void main(String[] args) {
        int[] arr = {3,4,6,1,8,2,9};

        bubbleSort(arr);
    }

   private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++){
            boolean flag = false;
            for (int j = 0; j < arr.length - 1 -i ; j++) {
                if (arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    flag = true;
                }
            }
            if (flag == false){
                break;
            }
        }
        printArr(arr);
    }

    private static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");

        }
        System.out.println();
    }
}
上一篇:Android PendingIntent


下一篇:JS如何实现点击页面其他地方隐藏菜单?