C#稀疏数组算法实现

当一个二维数组存在多个空值或无效值时,存入磁盘会浪费一定的系统资源,如下图
一个6行5列的二维数组,这个数组只有,1行2列,2行3列的位置存在有效值,其他的值是没有意义的。


image.png
压缩数组

把上述的数组进行压缩,得到一个压缩的数组并可以根据压缩的数组恢复原始数组

压缩后的数组有一定的规范,第一行为原始数组的行,列,有效值数量的描述(固定)
第二行是原始数组的行,列,有效值的描述(第几行,第几列,什么值)

除了第一行是对整个数组的行列描述,其他行是描述具体值的位置,所以这个压缩数组的行数是:有效值数量+1。

根据这个压缩数组,可以解压成原始数组

image.png

代码实现

原始数组

压缩

存盘

读盘

解压

using System;
using System.IO;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {

            int[,] a =new int[6,5];
            a[1, 2] = 1;
            a[2, 3] = 2;
            Console.WriteLine("---------------原始数组---------------");
            for (int i=0;i<6;i++) 
            {
                for(int j=0;j<5;j++) 
                {
                    Console.Write("   "+a[i,j]);                 
                }

                Console.WriteLine();
                Console.WriteLine();
            }



            //得到有效的数据
            int tem = 0;         
            for (int i = 0; i <=a.Rank; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (a[i,j]!=0)
                    {                        
                        tem++;
                    }
                }
            }

            //创建压缩数组
            int[,] b = new int[tem + 1, 3];
            b[0, 0] = 6;
            b[0, 1] = 5;
            b[0, 2] = tem;


           //给压缩赋值
            int count = 0;
            for (int i = 0; i <= a.Rank; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (a[i, j] != 0)
                    {
                        count++;
                       b[count,0]=i;
                       b[count,1]=j;
                       b[count,2]=a[i,j];
                    }
                }
            }
            //遍历压缩后的数组
            Console.WriteLine("---------------压缩后的数组---------------");
            OutArray(b);


            //压缩后的数组写入txt文件
            Console.WriteLine("---------------压缩后存入txt文件---------------");
            //System.Text.StringBuilder bui = null;
            //StreamWriter sw = new StreamWriter("D:\\ex.txt", true);
            //for (int i = 0; i <= b.Rank; i++)
            //{
            //    bui = new System.Text.StringBuilder();
            //    for (int j = 0; j <= b.Rank; j++)
            //    {
            //        bui.Append(b[i, j]);
            //    }
            //    sw.WriteLine(bui);
            //}
            //sw.Close();


            int row = 0;
            int[,] Array = null;
            StreamReader streamReader = new StreamReader("D:\\ex.txt");
            //判断文件中是否有字符
            while (streamReader.Peek() != -1)
            {
                //读取文件中的一行字符
                string str = streamReader.ReadLine();
                if (str != null)
                    row++;
            }
            streamReader.Close();

            StreamReader t = new StreamReader("D:\\ex.txt");
            Array = new int[row, 3];


            ////读取数据生成数组
            readdata(Array, t);
            Console.WriteLine("---------------文件读取结果-------------");
            for (int i = 0; i <= Array.Rank; i++)
            {
                for (int j = 0; j <= Array.Rank; j++)
                {
                    Console.Write("   " + Array[i, j]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }


            //数组稀疏
            //创建稀疏状态数组
            int[,] C = new int[Array[0,0], Array[0,1]];

            //给稀疏数组赋值
            for (int i = 1; i <= Array.Rank; i++)
            {
              C[Array[i, 0], Array[i, 0 + 1]] = Array[i, 2];              
            }



            Console.WriteLine("---------------解压缩后的数组---------------");

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write("   " + C[i, j]);
                }

                Console.WriteLine();
                Console.WriteLine();
            }
                Console.ReadLine();
        }


        //遍历数组
        public static void OutArray(int[,]par) 
        {
                for(int i=0;i<=par.Rank;i++)
                {     
                    for (int j = 0; j <=par.Rank; j++)
                    {
                        Console.Write("   " + par[i,j]);
                    }
                    Console.WriteLine();
                    Console.WriteLine();    
                }
                    Console.WriteLine();
        }


        //遍历数组文件并赋值
        public static void readdata(int[,] a, StreamReader file) 
        {      
            int i = 0;
            while (file.Peek() != -1)
            {
                int j = 0;
                string str = file.ReadLine();
                foreach (var c in str)
                {
                    a[i,j] = int.Parse(c.ToString());
                    if (j<3) { j++; }             
                }
                i++;
            }
        }



    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容