利用嵌套循环实现冒泡排序

重要的事情说三遍:练练手速,练练手速,练练手速

import java.util.Arrays;

/**
 * @author: Ren
 * @date: 2020-07-28  11:57
 */
public class C8 {
    public static void main(String[] args) {
        int[]array = {1,212,31,315,4,54,531};

//        从小到大
        sort(array,(j1, j2) -> j1-j2 );
        System.out.println(Arrays.toString(array));
        //从大到小
        sort(array,(j1, j2) -> j2-j1);
        System.out.println(Arrays.toString(array));

    }
    public static void sort(int []array,IntCompare compare){
        for (int i = 0;i<array.length-1;i++){
            for (int j =0;j<array.length-(i+1);j++) {
                if (compare.compare(array[j], array[j + 1]) > 0) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
            }
                System.out.println(Arrays.toString(array));
        }

}

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44281922/article/details/107700666