コード例 #1
0
        public static void Test_File_Array_List(int seed, int n)
        {
            Console.WriteLine("\n Išorinėje atmintyje suliejimo rykiavimas\n");
            string    filename;
            Stopwatch watch = new Stopwatch();

            filename = @"mydataarray.dat";
            MyFileArray sortedFileArray;
            MyFileArray myfilearray = new MyFileArray(filename, n, seed);

            Console.WriteLine("\n Masyvas \n");
            myfilearray.Print(n);
            watch.Start();
            sortedFileArray = (MyFileArray)MergeSort(myfilearray);
            watch.Stop();
            var arrayTime = watch.Elapsed;

            Console.WriteLine("\n Surykiuotas masyvas \n");
            sortedFileArray.Print(n);

            filename = @"mydatalist.dat";
            MyFileList sortedFileList;
            MyFileList myfilelist = new MyFileList(filename, n, seed);

            Console.WriteLine("\n Sąrašas \n");
            myfilelist.Print(n);
            watch.Start();
            sortedFileList = (MyFileList)MergeSort(myfilelist);
            watch.Stop();
            var listTime = watch.Elapsed;

            Console.WriteLine("\n išrykiuotas \n");
            sortedFileList.Print(n);
            Console.WriteLine("masyvo vykdymo laikas: " + arrayTime + " sąrašo vykdymo laikas: " + listTime);
        }
コード例 #2
0
        //-----------------------------------------------------
        //Masyvo rikiavimas
        private static DataArray MergeSort(DataArray unsorted)                      // kaina | kartai
        {
            if (unsorted.Length <= 1)
            {
                return(unsorted);                                                   //    c1  |  1
            }
            DataArray left;
            DataArray right;

            if (unsorted.GetType() == typeof(MyDataArray))
            {
                left  = new MyDataArray(unsorted.Length / 2);                      //    c2  |  1
                right = new MyDataArray(unsorted.Length / 2 + 1);                  //    c2  |  1
            }
            else
            {
                left  = new MyFileArray(unsorted.Length / 2);                      //    c2  |  1
                right = new MyFileArray(unsorted.Length / 2 + 1);                  //    c2  |  1
            }

            int middle = unsorted.Length / 2; //    c3  | 1

            for (int i = 0; i < middle; i++)  //Dividing the unsorted list
            {
                left.Add(unsorted[i]);
            }
            for (int i = middle; i < unsorted.Length; i++)
            {
                right.Add(unsorted[i]);
            }

            left  = MergeSort(left);
            right = MergeSort(right);
            return(Merge(left, right));
        }
コード例 #3
0
        public static void Test_Array_List_Comparison_File(int seed, int n)
        {
            Stopwatch   watch   = new Stopwatch();
            MyFileArray myarray = new MyFileArray("testasArray.dat", n, seed);

            watch.Start();
            MyFileArray MySortedArray = (MyFileArray)MergeSort(myarray);

            watch.Stop();
            var        arrayTime = watch.Elapsed;
            MyFileList mylist    = new MyFileList("testasList.dat", n, seed);

            watch.Start();
            MyFileList MySortedList = (MyFileList)MergeSort(mylist);

            watch.Stop();
            var listTime = watch.Elapsed;

            Console.WriteLine("{0, -10} {1, -20}  {2, -20}", n, arrayTime, listTime);
        }
コード例 #4
0
        private static DataArray Merge(DataArray left, DataArray right)
        {
            DataArray result;

            if (right.GetType() == typeof(MyDataArray))
            {
                result = new MyDataArray(left.Length + right.Length);
            }
            else
            {
                result = new MyFileArray(left.Length + right.Length);
            }
            while (left.Length > 0 || right.Length > 0)
            {
                if (left.Length > 0 && right.Length > 0)
                {
                    if (left.First() <= right.First()) //lyginam pirmus elementus
                    {
                        result.Add(left.First());      //idedam pirma
                        left.RemoveFirst();            //pasalinam is kairio pirma
                    }
                    else
                    {
                        result.Add(right.First());
                        right.RemoveFirst();
                    }
                }
                else if (left.Length > 0)
                {
                    result.Add(left.First());
                    left.RemoveFirst();
                }
                else if (right.Length > 0)
                {
                    result.Add(right.First());

                    right.RemoveFirst();
                }
            }
            return(result);
        }