"Data structure _ sparse array"

First, the concept

    (Transfer from concept blog:     https://blog.csdn.net/iteye_15605/article/details/82678731 )

    The so-called sparse array is an array of values ​​in most of the content are not being used (or are zero), only a small part of the use of space in the array. Therefore resulting in a waste of memory space, in order to save memory space, and does not affect any original content value array, we can use a compressed way to represent sparse array of content

    

  Suppose there is a 9 * 7 array, which reads as follows:

 

                  



  In this array, a total of 63 space, but only the five elements, resulting in a waste of 58 elements in space. Below we use a sparse array re-define the array:

 

                                              



Wherein a first portion of a sparse array is recorded and the number of columns and rows the number of elements used in the original array, the second portion of the record is the position of elements in the array and the original content.

  Incidentally, the number of elements is the number of columns of the second portion of the underlying column index set, with the actual number of columns is not the same as the first portion


  Wherein a first portion of a sparse array is recorded and the number of columns and rows the number of elements used in the original array, the second portion of the record is the position of elements in the array and the original content.

 

  The second part, for example, can be expressed as, assuming the array to array [] [], array [1] [1] = 3, array [3] [0] = 1 ... etc.

 

    After compression, the original need to declare an array of size 63, while the use of compression, just declare an array of size 6 * 3, only 18 storage space.

 

 

Two, Java code implementation

 

    

package DataStructur;

public class SparseArray
{


    public static void main(String[] args) {
        int [][] array = new int[11][11];
        array[1][2] = 1;
        array[2][2] = 2;
        array[4][4] = 1;
        int count = 0;
        for(int i = 0;i<11;i++){
            for (int j = 0;j<11;j++)
            {
                if(array[i][j]!=0){
                    count++;
                }
            }
        }
        int [][] sparseArray = new int [count+1][3];
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = count;
        int index  = 1;
        for(int i = 0;i<11;i++){
            for (int j = 0;j<11;j++)
            {
                if(array[i][j]!=0){
                    SparseArray [index] [ 0] = I; 
                    SparseArray [index] [ . 1] = J; 
                    SparseArray [index] [ 2] = Array [I] [J]; 
                    index ++ ; 
                } 
            } 
        } 

        // print the original array 
        System. out.println ( "original array:" );
         for ( int I = 0; I <. 11; I ++ ) 
        { 
            for ( int J = 0; J <. 11; J ++ ) 
            { 
                System.out.printf (array [I] [J] + "" ); 
            }
            System.out.println (); 
        } 

        System.out.println ( "sparse array created:" );
         // print created sparse array 
        for ( int I = 0; I <+ COUNT. 1; I ++ ) 
        { 
            the System.out .printf ( "\ T% D \ D% T \ T% D \ n-", SparseArray [I] [0], SparseArray [I] [. 1], SparseArray [I] [2 ]); 
        } 

        int [] [ ] = newArray new new  int [SparseArray [0] [0]] [SparseArray [0] [. 1 ]]; 

        System.out.println ( "sparse array restore the original array:" );
         // restore the original array 
        for ( int I =. 1; I <+ COUNT. 1; I ++ )
        {
            newArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }

        //打印新创建的数组
        for(int i = 0;i<11;i++)
        {
            for(int j = 0;j<11;j++)
            {
                System.out.printf(newArray[i][j]+" ");
            }
            System.out.println();
        }






    }
}

 

 

Overall, this data structure is not too difficult, just look carefully to understand the basic idea can write about. Hanshun Ping teacher data structures course there is a job, so that the stored data to a file and then reloading after reading from a file generates an array, to leave the pit.

 

 

Forget that the use of this data structure of the scene, can be used in the current chess game of backgammon game, you can save, next time read directly.

Guess you like

Origin www.cnblogs.com/kangxinxin/p/10958605.html