Ejemplo n.º 1
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();
                }
            }
        }
Ejemplo n.º 2
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> dataTime = new List <string>();
            List <string> dataOps  = new List <string>();

            int[] amount    = { 100000, 150000, 200000, 250000, 300000, 400000, 500000, 750000, 1000000, 1250000, 1500000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000 };
            int   repeat    = 10;
            bool  print     = false;
            long  startTime = 0;
            long  endTime   = 0;

            // int n = 100000;
            dataTime.Add("ARRAY\n");
            dataOps.Add("ARRAY\n");
            Console.WriteLine("\n ARRAY \n");
            foreach (int n in amount)
            {
                Console.WriteLine("n = " + n);
                for (int i = 0; i < repeat; i++)
                {
                    MyDataArray myarray = new MyDataArray(n, seed);
                    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));
                    dataTime.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                if (merge)
                {
                    ulong operations = MergeSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                if (bucket)
                {
                    ulong operations = BucketSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                dataTime.Add("");
            }

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

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

            dataTime.Add("\n\n\nLIST\n");
            dataOps.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++)
                {
                    MyDataList mylist = new MyDataList(n, seed);
                    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));
                    dataTime.Add($"{n};{(float)TimeSpan.FromTicks(endTime - startTime).TotalMilliseconds / 1000.0f}");
                }
                if (merge)
                {
                }
                if (bucket)
                {
                    ulong operations = BucketSortOperations((ulong)n);
                    dataOps.Add($"{n};{operations}");
                }
                dataTime.Add("");
            }
            l += 5 + amount.Length;
            foreach (int n in amount)
            {
                dataTime.Add($"{n};=AVERAGE(B{l}:B{l + repeat - 1})");
                l += repeat + 1;
            }
            if (merge)
            {
                File.WriteAllLines("Merge_Times.csv", dataTime);
                File.WriteAllLines("Merge_Operations.csv", dataOps);
            }
            if (bucket)
            {
                File.WriteAllLines("Bucket_Times.csv", dataTime);
                File.WriteAllLines("Bucket_Operations.csv", dataOps);
            }
        }