2022年2月6日

冒泡排序

时间复杂度是O(n^2)

public class GuLuGuLuGuLu {
    public static void main(String[] args) {
        int[] array = {1,32,34,6,37,31,74,8,9,6};
        printArray(array);
        GuLuGuLuGuLuGuLusort(array);
        printArray(array);
    }

    //冒泡排序
    /*
    1、比较两个相邻的元素,如果第一个比第二个大,则交换位置
    2、每次比较都会产生一个最大或者最小的数字
    3、下一轮就可以少一次排序
    4、依次循环直到结束
     */
    public static void GuLuGuLuGuLuGuLusort(int[] array){
        //外层循环决定走多少次
        for (int i = 0; i < array.length - 1; i++) {
            //内层循环比较元素
            for(int j = 0; j < array.length - 1 - i;j++){
                if(array[j] > array[j + 1]){
                    int x = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = x;
                }
            }
        }
    }

    //遍历数组
    public static void printArray(int[] array){
        for (int i : array) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }
}

稀疏数组

    在某些情况下,二维数组会存放很多默认值0,比如五子棋
    因此记录了很多没有意义的数据
    解决:压缩算法,稀疏数组
 
    当一个数组中大部分元素是0,或者是同一值时,可以用稀疏数组来保存该数组
    
    稀疏数组的处理方式:
    1、记录数组行列数,有多少不同值
    2、把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

在这里插入图片描述
图源B站狂神

public class SparseArray {
    public static void main(String[] args) {
        //原始数组
        int[][] array = new int[11][11];
        array[1][2] = 1;
        array[2][3] = 2;
        array[3][7] = 1;
        System.out.println("输出原始数组");
        printDimensionalArray(array);
        System.out.println("====================================");

        //稀疏数组
        //1、获取有效值个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for(int j = 0; j < 11; j++){
                if(array[i][j] != 0) sum++;
            }
        }
        System.out.println("有效值个数"+sum);
        //2、创建稀疏数组
        int[][] bubble = new int[sum + 1][3];
        //x个有效值就是x+1行,永远都是3列
        bubble[0][0] = 11;
        bubble[0][1] = 11;
        bubble[0][2] = sum;
        //3、遍历原始数组,将非零值存放到稀疏数组
        int count = 0;//计数器
        for (int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++){
                if(array[i][j] != 0){
                    bubble[++count][0] = i;//保存行
                    bubble[count][1] = j;//保存列
                    bubble[count][2] = array[i][j];//保存值
                }
            }
        }
        //4、输出稀疏数组
        System.out.println("输出稀疏数组");
        printDimensionalArray(bubble);
        System.out.println("====================================");
        //5、通过稀疏数组还原数组
        int[][] newArray = new int[bubble[0][0]][bubble[0][1]];
        //newArray[bubble[1][0]][bubble[1][1]] = bubble[1][2];
        //newArray[bubble[2][0]][bubble[2][1]] = bubble[2][2];
        for (int i = 1; i < bubble.length; i++) {
            newArray[bubble[i][0]][bubble[i][1]] = bubble[i][2];
        }
        System.out.println("输出还原的数组");
        printDimensionalArray(newArray);
    }

    public static void printDimensionalArray(int[][] array){
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        /*
        for (int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++){
                System.out.print(array[i][j] + "\t");
            }
            System.out.println();
        }
         */
    }
}

运行结果:
在这里插入图片描述

面向对象编程OOP

Object Oriented Programing
明天再说

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值