JAVA data structure-1. Sparse array introduction and use cases

Data structure and algorithm learning

Learn the data structure and algorithm from the perspective of JAVA, and analyze the JDK source code implementation while implementing the algorithm.

1. Sparse array

​ When most of the elements in an array are 0 or an array of the same value, a sparse array can be used to save the array.

Approach:

  1. The first column of the record array has several rows and columns, and how many different values ​​are there.
  2. Record the ranks and values ​​of elements with different values ​​in a small array (sparse array) to reduce the size of the program.
  3. Therefore, the sparse array is always an array with N rows and 3 columns, and N is the number of different values ​​in the original two-dimensional array + 1.

Insert picture description here

Implementation ideas:

Two-dimensional array to a sparse array:

  1. Analyze and traverse the two-dimensional array to find the number of different values ​​sum;
  2. Create a sparse array sparseArr int according to the number of different values ​​sum [sum+1][3];
  3. Store the valid data of a two-dimensional array into a sparse array.

Sparse array to a two-dimensional array:

  1. Analyze the row and col attributes of the first row of the sparse array, and create the original two-dimensional array arr = int [row][col];
  2. Read the data in the last few rows of the sparse array and assign it to the two-dimensional array.

Code:

/**
 * 稀疏数组实现
 */
public class SparseArry {
    
    
    public static void showArray(int [][] arr){
    
    
        for (int [] clo: arr){
    
    
            for (int data: clo){
    
    
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
    }
    public  static void main(String args[]){
    
    
        //二维数组初始化
        int[][] arr= new int[11][11];
        arr[2][3] = 1;
        arr[3][4] = 2;
        arr[5][8] = 3;
        System.out.println("原始二维数组");
        SparseArry.showArray(arr);

        /**
         * 将二维数组转换为稀疏数组
         *  1. 分析遍历二维数组,找出不同值的个数sum;
            2. 根据不同值的个数sum创建稀疏数组 sparseArr  int`[sum+1][3]`;
            3. 将二维数组的有效数据存入稀疏数组.
         */
//         1. 分析遍历二维数组,找出不同值的个数sum;
        int sum = 0;
        for (int [] clo: arr){
    
    
            for (int data: clo){
    
    
                if(data != 0){
    
    
                    sum ++;
                }
            }
        }
        System.out.println("有效值的个数:sum="+sum);
//        2. 根据不同值的个数sum创建稀疏数组 sparseArr  int`[sum+1][3]`;
        int [][] sparseArr = new int [sum+1][3];
//        3. 将二维数组的有效数据存入稀疏数组.
            //3.1第一行存储整个二维数组基本信息
        sparseArr[0][0] = arr.length;
        sparseArr[0][1] = arr[0].length;
        sparseArr[0][2] = sum;
            //3.2将二维数组的其他有效数据存入稀疏数组
        int count = 0;
        for (int i = 0; i <arr.length ; i++) {
    
    
            for (int j = 0; j <arr[i].length ; j++) {
    
    
                if(arr[i][j] != 0){
    
    
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = arr[i][j];
                }
            }
        }
        System.out.println("转换后的稀疏数组为");
        showArray(sparseArr);
        
        /**
         *将稀疏数组转换为二维数组
         * 1. 分析稀疏数组的第一行的row和col属性,创建原始的二维数组  arr = int`[row][col]`;
            2. 读取稀疏数组后几行的数据,赋值给二维数组即可.
         */
//        1. 分析稀疏数组的第一行的row和col属性,创建原始的二维数组  arr = int`[row][col]`;
        int [][] rarr = new int[sparseArr[0][0]][sparseArr[0][1]];
//        2. 读取稀疏数组后几行的数据,赋值给二维数组即可.
        for (int i = 1; i <sparseArr.length ; i++) {
    
    
            rarr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        System.out.println("稀疏数组转换回二维数组");
        showArray(rarr);
    }
}

Guess you like

Origin blog.csdn.net/weixin_44634197/article/details/108308052