Esempio n. 1
0
        public static void TestArray_ListFile(int seed)
        {
            int         n        = 12;
            string      filename = @"myTestArray.dat";
            MyFileArray myarray  = new MyFileArray(filename, n, seed);

            using (myarray.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Console.WriteLine("---FileArray---");
                myarray.Print(n);
                Console.WriteLine();
                Console.WriteLine("---HeapSortedFileArray---");
                HeapSortArray.HeapSortas(myarray);
                myarray.Print(n);
                Console.WriteLine();
            }
            filename = @"myTestList.dat";
            MyFileList mylist = new MyFileList(filename, n, seed);

            using (mylist.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Console.WriteLine("---FileList---");
                mylist.Print(n);
                Console.WriteLine();
                Console.WriteLine("---HeapSortedFileList---");
                HeapSortList.HeapSortas(mylist);
                mylist.Print(n);
                Console.WriteLine();
            }
        }
        public static void Test_D(int seed)
        {
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("Test D");

            Stopwatch sw = new Stopwatch();

            string filename = @"/home/justin/Projects/HeapSort_OP/HeapSort_OP/mydataarray.dat";

            sw.Start();
            Console.WriteLine("Size of {0} count was initialized in => {1}", n, sw.Elapsed);
            MyFileArray myfilearray = new MyFileArray(filename, n, seed);

            sw.Stop();

            sw.Start();

            using (myfilearray.fs = new FileStream(filename, FileMode.Open,
                                                   FileAccess.ReadWrite))
            {
                Console.WriteLine("\n FILE ARRAY \n");
                myfilearray.Print(n);
                Heapsort_D.HeapSort(myfilearray, n);

                myfilearray.PrintFromFile(n);
            }
            sw.Stop();

            Console.WriteLine("Test D success ");
            Console.WriteLine("Size of {0} count file was sorted in => {1}", n, sw.Elapsed);
            Console.WriteLine("---------------------------------------------");
        }
Esempio n. 3
0
        // To heapify a subtree rooted with node i which is
        // an index in arr[]. n is size of heap
        void heapify(MyFileArray arr, int n, int i)
        {
            int largest = i;         // Initialize largest as root
            int l       = 2 * i + 1; // left = 2*i + 1
            int r       = 2 * i + 2; // right = 2*i + 2

            // If left child is larger than root
            if (l < n && 1 == Comparer <IntFloat> .Default.Compare(arr[l], arr[largest]))
            {
                largest         = l;
                OperationCount += 1;
            }
            // If right child is larger than largest so far
            if (r < n && 1 == Comparer <IntFloat> .Default.Compare(arr[r], arr[largest]))
            {
                largest         = r;
                OperationCount += 1;
            }
            OperationCount += 4;
            // If largest is not root
            OperationCount += 1;
            if (largest != i)
            {
                IntFloat swapp = arr[i];
                Swap(i, largest, arr);           //arr[i] = arr[largest];
                arr.WriteToFile(largest, swapp); //arr[largest] = swapp;
                OperationCount += 4;
                // Recursively heapify the affected sub-tree
                heapify(arr, n, largest);
            }
        }
Esempio n. 4
0
        public void sort(MyFileArray arr, int length)
        {
            int n = length;

            OperationCount += 1;
            // Build heap (rearrange array)
            for (int i = n / 2 - 1; i >= 0; i--)
            {
                heapify(arr, n, i);
                OperationCount += 2;
            }

            // One by one extract an element from heap
            for (int i = n - 1; i >= 0; i--)
            {
                // Move current root to end
                IntFloat temp = arr[0];
                Swap(i, 0, arr);          //arr[0] = arr[i];
                arr.WriteToFile(i, temp); //arr[i] = temp;

                // call max heapify on the reduced heap
                heapify(arr, i, 0);
                OperationCount += 5;
            }
        }
        public static void Test_D(int seed, int n)
        {
            Stopwatch sw = new Stopwatch();

            string      filename    = @"/home/justin/Projects/HeapSort_OP/HeapSort_OP/mydataarray.dat";
            MyFileArray myfilearray = new MyFileArray(filename, n, seed);

            sw.Start();

            using (myfilearray.fs = new FileStream(filename, FileMode.Open,
                                                   FileAccess.ReadWrite))
            {
                Heapsort_D.HeapSort(myfilearray, n);
            }
            sw.Stop();

            Console.WriteLine("{1,9} took => {0}", sw.Elapsed, n);
        }
Esempio n. 6
0
        public static void AnalysisFILEArray_List(int seed, int kiek)
        {
            string filename = @"myTestArray.dat";
            int    n        = duomskc;

            Console.WriteLine("FileArray HeapSort");
            for (int i = 0; i < kiek; i++)
            {
                MyFileArray myarray = new MyFileArray(filename, n, seed);
                Stopwatch   timer   = new Stopwatch();
                using (myarray.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
                {
                    timer.Start();
                    Program.HeapSortArray.HeapSortas(myarray);
                    timer.Stop();
                }
                //myarray.Print(n);
                Console.WriteLine("{0,-10}{1}", n, timer.Elapsed);
                n = n * 2;
            }
            Console.WriteLine();
            filename = @"myTestList.dat";
            n        = duomskc;
            Console.WriteLine("FileList HeapSort");
            Console.WriteLine("N         RunTime");
            for (int i = 0; i < kiek; i++)
            {
                MyFileList mylist = new MyFileList(filename, n, seed);
                Stopwatch  timer  = new Stopwatch();
                using (mylist.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
                {
                    timer.Start();
                    Program.HeapSortList.HeapSortas(mylist);
                    timer.Stop();
                }
                //mylist.Print(n);
                Console.WriteLine("{0,-10}{1}", n, timer.Elapsed);
                n = n * 2;
            }
            Console.WriteLine();
            Console.WriteLine("---PABAIGA---");
        }
Esempio n. 7
0
        public void Swap(int j, int i, MyFileArray arr)
        {
            IntFloat a = arr[j];
            IntFloat b = arr[i];

            Byte[] data = new Byte[24];
            fs.Seek(i * 12, SeekOrigin.Begin);
            data = BitConverter.GetBytes((double)a.Floater);
            fs.Write(data, 0, 8);
            fs.Seek((i * 12) + 8, SeekOrigin.Begin);
            data = BitConverter.GetBytes(a.Integer);
            fs.Write(data, 0, 4);
            fs.Seek(j * 12, SeekOrigin.Begin);
            data = BitConverter.GetBytes((double)b.Floater);
            fs.Write(data, 0, 8);
            fs.Seek((j * 12) + 8, SeekOrigin.Begin);
            data = BitConverter.GetBytes(b.Integer);
            fs.Write(data, 0, 4);
            OperationCount += 14;
        }
Esempio n. 8
0
        public void HeapSortDISK(int NumberOfElements)
        {
            int startInt = 0; int finishInt = 0;

            Console.WriteLine("\nSorting in DISK memory");
            long        time;
            long        time2;
            int         ArrayOP;
            int         ListOP;
            Stopwatch   x = new Stopwatch();
            MyFileArray b = new MyFileArray("array.dat", NumberOfElements * 12);
            MyFileList  a = new MyFileList("test.dat", NumberOfElements * 12);

            Console.WriteLine("Ar norite pamatyti DISKINĖJE atmintyje sugeneruotas aibes? Įvesti: Y/N");
            string YN  = Console.ReadLine();
            bool   yes = false;

            if (YN == "y" || YN == "Y")
            {
                yes = true;
            }

            if (yes)
            {
                Console.WriteLine("Nurodykite intervala, kuriuos elementus norite pamatyti:");
                Console.WriteLine("Nuo: ");
                startInt = int.Parse(Console.ReadLine());
                Console.WriteLine("Iki: ");
                finishInt = int.Parse(Console.ReadLine());
                Console.WriteLine("LIST AIBE:");
                for (int i = startInt - 1; i < finishInt - 1; i++)
                {
                    Console.WriteLine($"{(i + 1)}. " +
                                      a.GetByIndex(i).ToString());
                }
                Console.WriteLine();
                Console.WriteLine("ARRAY AIBE:");
                for (int i = startInt - 1; i < finishInt - 1; i++)
                {
                    Console.WriteLine($"{(i + 1)}. " + b[i].ToString());
                }
            }
            Console.WriteLine("Ar norite pamatyti surikiuotas aibes? Y/N");
            string rikiuotiYN   = Console.ReadLine();
            bool   rikiuotiAibe = false;

            if (rikiuotiYN == "Y" || rikiuotiYN == "y")
            {
                rikiuotiAibe = true;
            }

            x.Reset();
            x.Start();
            b.sort(b, NumberOfElements);
            x.Stop();
            time2   = x.ElapsedMilliseconds;
            ArrayOP = b.getOpCount();
            Console.WriteLine($"Finished ARRAY sorting items, elapsed time: {(double)time2/1000}ms  | Operations made: {ArrayOP}");

            Console.WriteLine("---------------------");

            x.Reset();
            x.Start();
            a.startSort(NumberOfElements);
            x.Stop();
            time   = x.ElapsedMilliseconds;
            ListOP = a.getOpCount();
            Console.WriteLine($"Finished LIST sorting items, elapsed time: {(double)time/1000}ms | Operations made: {ListOP}");
            Console.WriteLine("--------------------");
            if (rikiuotiAibe)
            {
                Console.WriteLine("Nurodykite intervala, kuriuos elementus norite pamatyti:");
                Console.WriteLine("Nuo:");
                int pradzia = int.Parse(Console.ReadLine());
                Console.WriteLine("Iki:");
                int pabaiga = int.Parse(Console.ReadLine());
                Console.WriteLine("Diske surikiuotas masyvas:");
                for (int i = pradzia - 1; i < pabaiga - 1; i++)
                {
                    Console.WriteLine(b[i].ToString());
                }
                Console.WriteLine("Diske surikiuotas LIST:");
                for (int i = pradzia - 1; i < pabaiga - 1; i++)
                {
                    Console.WriteLine(a.GetByIndex(i).ToString());
                }
            }
            b.Close();
            a.Close();
            Console.WriteLine($"Items: {NumberOfElements} ===> List: {(double)time/1000} ms | Operations: {ListOP}  vs  Array: {(double)time2/1000} ms | Operations: {ArrayOP}");
            Console.ReadKey();
        }