Sparse array

Time:2021-11-26

For an array, if most elements are 0 or other same values and there are only a few different values, the array can be converted into a sparse array for storage, so as to reduce the size of the array and realize the function similar to compression.

Convert to sparse array

Take the conversion of common two-dimensional arrays to sparse arrays as an example. The specific steps are as follows:

  1. Traverse the source array, obtain the number of valid data, and save it to the variable sum.
  2. Create a sparse array. The number of rows is sum + 1 and the number of columns is 3 columns. The three elements of the first row are the number of rows, columns and sum of the source array, and each subsequent row is the position and value of valid data in the source array.
public static int[][] arrayToSparseArray(int[][] arr){
        //1. Get the number of elements that are not 0 in the array
        int arrHeight = arr.length;
        int arrLength = arr[0].length;
        int sum = 0;
        for (int i = 0; i < arrHeight; i++) {
            for (int j = 0; j < arrLength; j++) {
                if (arr[i][j] != 0) {
                    sum++;
                }
            }
        }

        //2. Create a sparse array. The number of columns is 3 and the number of rows is sum + 1. The first row stores the length of the original array, and the following row stores the position and value of the original array that is not 0
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = arrHeight;
        sparseArr[0][1] = arrLength;
        sparseArr[0][2] = sum;

        //3. Store the values that are not 0 in the original array into the sparse array
        int count = 1;
        for (int i = 0; i < arrHeight; i++) {
            for (int j = 0; j < arrLength; j++) {
                if (arr[i][j] != 0) {
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = arr[i][j];
                    count++;
                }
            }
        }
        return sparseArr;
    }

Convert to 2D array

The steps to convert a sparse array into a two-dimensional array are as follows:

  1. Get the data of the first row of the sparse array and create a two-dimensional array.
  2. Obtain the subsequent data of the sparse array and assign it to the corresponding elements of the two-dimensional array.
public static int[][] sparseArrayToArray(int[][] sparseArr){
        //1. Get the value of the first row from the sparse array to create the array
        int arrHeight = sparseArr[0][0];
        int arrLength = sparseArr[0][1];
        int[][] arr = new int[arrHeight][arrLength];

        //2. Get the value from the sparse array and assign it to the newly created array
        for (int i = 1; i < sparseArr.length; i++) {
            arr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        return arr;
    }

test

public static void main(String[] args) {
        //Create a 2D array
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;
        System. Out. Println ("two dimensional array is:");
        for (int[] row : chessArr1) {
            for (int i : row) {
                System.out.printf("%d\t",i);
            }
            System.out.println();
        }

        System. Out. Println ("convert to sparse array:");
        int[][] parseArray = SparseArray.arrayToSparseArray(chessArr1);
        for (int[] row : parseArray) {
            for (int i : row) {
                System.out.printf("%d\t",i);
            }
            System.out.println();
        }

        System. Out. Println ("convert to 2D array:");
        int[][] sparseArrayToArray = SparseArray.sparseArrayToArray(parseArray);
        for (int[] row : sparseArrayToArray) {
            for (int i : row) {
                System.out.printf("%d\t",i);
            }
            System.out.println();
        }
    }

The output is as follows:

The two-dimensional array is:
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    2    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
Convert to sparse array:
11    11    3    
1    2    1    
2    3    2    
4    5    2    
Convert to 2D array:
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    2    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0

Welcome to my official account and learn technology together.

Sparse array