Sparse arrays of Java data structures and algorithms (2)

1. Definition

Sparse arrays can be regarded as the compression of ordinary two-digit arrays, but the ordinary arrays mentioned here are arrays whose value invalid data is much larger than the effective data. There are five children's checkers, maps, etc. about the use of sparse arrays .

* When most of the elements in an array are 0 or an array with the same value , you can use a sparse array to save the array

Sparse arrays of Java data structures and algorithms (2)

 

2. Benefits

* There are a lot of invalid data in the original array , occupying a lot of storage space, but there are very few really useful data

* Record the ranks and values ​​of elements with different values in a small-scale array, thereby reducing the size of the program

* Compressed storage can save storage space to avoid unnecessary waste of resources. Compressed storage can improve IO efficiency when data is serialized to disk

3. Structure of sparse array

* Record array has several rows and columns, how many different values

* The first row stores the total number of original data rows, the total number of columns, the total number of non-zero data

* The next row stores the row, column, and specific value of the non-zero number (as shown below)

Sparse arrays of Java data structures and algorithms (2)

Both rows and columns are 11 binary arrays

Sparse arrays of Java data structures and algorithms (2)

Convert the above two-dimensional array to a sparse array

Remarks: Because the subscripts of the array start from 0, so their labels also start from 0 (important)

4. The idea of ​​converting two-dimensional array to sparse array

* Traverse the original two-dimensional array to get the number of valid data sum

* According to sum, you can create a sparse array sparseArr int [sum + 1] [3]

* Save the effective data of the two-dimensional array to the sparse array

5. The idea of ​​converting sparse array to original two-dimensional array

* Read the first row of the sparse array first, and create the original two-dimensional array based on the data in the first row.

For example, the above chessArr2 = int [11] [11]

* After reading the data of the last few lines of the sparse array, and assign it to the original two-dimensional array

6. Code implementation

1  public  class Sparsearray {
 2  
3      public  static  void main (String [] args) {
 4          System.out.println ("=========== Two-dimensional array conversion sparse array ======= ===== " );
 5          int [] [] array = new  int [11] [11 ];
 6          array [1] [2] = 1 ;
 7          array [2] [3] = 2 ;
 8          System. out.println ("============ Two-dimensional array before conversion ============" );
 9          forToarray (array);
 10          System.out.println ("============ converted two-dimensional array ============" );
 11          int[] [] sparsearry = arrayToSparsearry (array);
 12          forToarray (sparsearry);
 13          System.out.println ("=========== Convert sparse array to original array ======= ===== " );
 14          array = sparsearryToArray (sparsearry);
 15          forToarray (array);
 16          
17      }
 18      / ** 
19       * Two-dimensional array conversion sparse array
 20       * @param array
 21       * / 
22      private  static  int [ ] [] arrayToSparsearry ( int [] [] array) {
 23          int sum = 1 ;
 24          for (int i = 0; i < array.length; i++) {
25             int[] datas=array[i];
26             for (int j = 0; j < datas.length; j++) {
27                 if (array[i][j]!=0) {
28                     sum++;
29                 }
30             }
31         }
32         
33         int [][] sparse=new int[sum][3];
34         int s=0;
35         sparse[s][0]=array.length;
36         sparse[s][1]=array[0].length;
37         sparse[s][2]=sum-1;
38         for (int i = 0; i < array.length; i++) {
39             int[] datas=array[i];
40             for (int j = 0; j < datas.length; j++) {
41                 int a=array[i][j];
42                 if (a!=0) {
43                     s++;
44                     sparse[s][0]=i;
45                     sparse[s][1]=j;
46                     sparse[s][2]=a;
47                 }
48             }
49         }
50 
51         return sparse;
52     }
53     /**
54      *遍历数组
55      * @param array
56      */
57     private static void forToarray(int[][] array) {
58         for (int i = 0; i < array.length; i++) {
59             int [] ints=array[i];
60             for (int j = 0; j <ints.length; j ++ ) {
 61                  System.out.print ("" + array [i] [j]);
 62              }
 63              System.out.println ("" );
 64          }
 65      }
 66       / * 
67        * Sparse array is converted to original array
 68       * @param array
 69       * / 
70      private  static  int [] [] sparsearryToArray ( int [] [] array) {
 71          int [] [] arrays = new  int [array [ 0] [0]] [array [0] [1 ]];
 72              for (int i = 1; i < array.length; i++) {
73                 int[] js = array[i];
74                 for (int j = 0; j < js.length; j++) {
75                     arrays[array[i][0]][array[i][1]]=array[i][2];
76                 }
77                 
78             }
79         return arrays;
80     }
81 }

* The results after execution are as follows

=========== Two-dimensional array conversion sparse array ============ 
============ Two-dimensional array before conversion === ========= 
  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 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 0 0 0 0 0 
============ Converted two-dimensional array == ========== 
  11 11 2 
  1 2 1 
  2 3 2 
=========== Convert sparse array to original array ============  
  0 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  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  0  0
  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0

 

Guess you like

Origin www.cnblogs.com/gzxg/p/12687975.html