冒泡排序的升序(正序)和降序(倒序)

升序&降序:

public static void main(String[] args) {
        float[] arr = {2.1F, 15.34F, 41.22F, 67F, 29.33F, 38.7F,12,3,1};
        System.out.println("开始咯");
        for (float item : arr) {
            System.out.print(item + "\t");
        }
        System.out.println();
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    float temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("倒序");
        for (float value : arr) {
            System.out.print(value + "\t");
        }

        System.out.println();
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] < arr[j + 1]) {
                    float temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("正序");
        for (float value : arr) {
            System.out.print(value + "\t");
        }
        System.out.println("结束啦");
    }
# java   优化   排序  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×