Exemple #1
0
        public static void Test_File_Array_List(int seed)
        {
            if (!Directory.Exists("tmp"))
            {
                Directory.CreateDirectory("tmp");
            }
            bool   print = false;
            long   startTime, endTime;
            int    n = 120;
            string filename;

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

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

                startTime = DateTime.Now.Ticks;
                BucketSort(myfilearray);
                endTime = DateTime.Now.Ticks;

                if (print)
                {
                    myfilearray.Print(n);
                }

                Console.WriteLine("Is sorted: " + myfilearray.IsSorted());
                Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
            }
            filename = @"mydatalist.dat";
            MyFileList myfilelist = new MyFileList(filename, n, seed);

            using (myfilelist.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Console.WriteLine("\n FILE LIST \n");
                if (print)
                {
                    myfilelist.Print(n);
                }

                startTime = DateTime.Now.Ticks;
                myfilelist.BucketSort();
                endTime = DateTime.Now.Ticks;

                if (print)
                {
                    myfilelist.Print(n);
                }

                Console.WriteLine("Is sorted: " + myfilelist.IsSorted());
                Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
            }
        }
Exemple #2
0
        static void Sort()
        {
            char key = GetAction(true);

            if (key == '0')
            {
                return;
            }
            char sort;

            while (true)
            {
                Console.WriteLine("\n1 - MergeSort;  2 - BucketSort;");
                sort = Console.ReadKey().KeyChar;
                if (sort == '1' || sort == '2')
                {
                    break;
                }
            }
            if (sort == '1')
            {
                if (key == '1' || key == '3')
                {
                    MergeSort(GlobalArray);
                }
                if (key == '2' || key == '3')
                {
                    GlobalList.MergeSort();
                }
            }
            if (sort == '2')
            {
                if (key == '1' || key == '3')
                {
                    BucketSort(GlobalArray);
                }
                if (key == '2' || key == '3')
                {
                    GlobalList.BucketSort();
                }
            }
        }
Exemple #3
0
        public static void Test_Array_List(int seed)
        {
            bool merge  = false;
            bool bucket = false;

            Console.WriteLine("Press M to MergeSort\nPress B to BucketSort\nPress Q to quit\n");
            bool input = true;

            while (input)
            {
                var key = Console.ReadKey();
                switch (char.ToUpper(key.KeyChar))
                {
                case 'M':
                    merge = true;
                    input = false;
                    Console.WriteLine("\n\nPerforming MergeSort...\n");
                    break;

                case 'B':
                    bucket = true;
                    input  = false;
                    Console.WriteLine("\n\nPerforming BucketSort...\n");
                    break;

                case 'Q':
                    return;
                }
            }

            System.Threading.Thread.Sleep(1000);

            if (merge && bucket)
            {
                Console.WriteLine("Cannot perform merge sort and bucket sort at the same time");
                return;
            }

            List <string> data = new List <string>();

            int[] amount    = { 100, 250, 500, 1000, 1500, 2000, 2500, 3000 };
            int   repeat    = 10;
            bool  print     = false;
            long  startTime = 0;
            long  endTime   = 0;

            // int n = 100000;
            data.Add("ARRAY\n");
            Console.WriteLine("\n ARRAY \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyFileArray myarray = new MyFileArray("mydataarray.dat", n, seed);
                    using (myarray.fs = new FileStream("mydataarray.dat", FileMode.Open, FileAccess.ReadWrite))
                    {
                        if (print)
                        {
                            myarray.Print(n);
                        }
                        GC.Collect();
                        if (merge)
                        {
                            startTime = DateTime.Now.Ticks;
                            MergeSort(myarray);
                            endTime = DateTime.Now.Ticks;
                        }
                        if (bucket)
                        {
                            startTime = DateTime.Now.Ticks;
                            BucketSort(myarray);
                            endTime = DateTime.Now.Ticks;
                        }
                        if (print)
                        {
                            myarray.Print(n);
                        }
                        Console.WriteLine("Is sorted: " + myarray.IsSorted());
                    }
                    Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));

                    data.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                data.Add("");
            }

            int line = 2 + amount.Length + (repeat * amount.Length);
            int l    = 3;

            foreach (int n in amount)
            {
                data.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }

            data.Add("\n\n\nLIST\n");
            Console.WriteLine("\n LIST \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyFileList mylist = new MyFileList("mydatalist.dat", n, seed);
                    using (mylist.fs = new FileStream("mydatalist.dat", FileMode.Open, FileAccess.ReadWrite))
                    {
                        if (print)
                        {
                            mylist.Print(n);
                        }
                        GC.Collect();
                        if (merge)
                        {
                            startTime = DateTime.Now.Ticks;
                            mylist.MergeSort();
                            endTime = DateTime.Now.Ticks;
                        }
                        if (bucket)
                        {
                            startTime = DateTime.Now.Ticks;
                            mylist.BucketSort();
                            endTime = DateTime.Now.Ticks;
                        }
                        if (print)
                        {
                            mylist.Print(n);
                        }
                        Console.WriteLine("Is sorted: " + mylist.IsSorted());
                    }
                    Console.WriteLine("Elapsed time in ticks: " + (endTime - startTime));
                    data.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                data.Add("");
            }
            l += 5 + amount.Length;
            foreach (int n in amount)
            {
                data.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }
            if (merge)
            {
                File.WriteAllLines("Merge_Times.csv", data);
            }
            if (bucket)
            {
                File.WriteAllLines("Bucket_Times.csv", data);
            }
        }