Introduction to Sparse Arrays

The so-called sparse array is that most of the content values ​​in the array are unused (or all zero), and only a small part of the space in the array is used. Therefore, the memory space is wasted. In order to save the memory space and not affect the original content value in the array, we can use a compression method to represent the content of the sparse array.

  Suppose there is a 9*7 array with the following contents:

 





In this array, there are 63 spaces in total, but only 5 elements are used, resulting in a waste of 58 elements of space. Let's redefine this array using a sparse array:

 



It should be noted that the number of element columns in the second part is the number of columns in the index group, which is different from the actual number of columns in the first part.

The first part of the sparse array records the number of columns and rows of the original array and the number of elements used, and the second part records the position and content of the elements in the original array.

 

For example, the second part of the content can be expressed as, assuming the array is array[][], array[1][1]=3, array[3][0]=1...etc

 

After compression, an array of size 63 needs to be declared, but after compression, only an array of size 6*3 needs to be declared, and only 18 storage spaces are needed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711002&siteId=291194637
Recommended