コード例 #1
0
        public MainClass()
        {
            HeapSort       heapSort = new HeapSort();
            CombSort       CombSort = new CombSort();
            ArrayGenerator arrays   = new ArrayGenerator();

            int[] array1 = arrays.generateRandomArray(10);
            int[] array2 = arrays.generateRandomArray(100);
            //int[] array3 = arrays.generateRandomArray(1000);
            //int[] array4 = arrays.generateRandomArray(10000);
            // int[] array5 = arrays.generateRandomArray(100000);
            //   int[] array6 = arrays.generateRandomArray(1000000);


            DateTime time1 = DateTime.Now;

            heapSort.sort(array1);
            DateTime time2 = DateTime.Now;
            TimeSpan total = new TimeSpan(time2.Ticks - time1.Ticks);

            Console.WriteLine(total.TotalMilliseconds + "");

            DateTime time3 = DateTime.Now;

            heapSort.sort(array2);
            DateTime time4  = DateTime.Now;
            TimeSpan total1 = new TimeSpan(time3.Ticks - time4.Ticks);

            Console.WriteLine(total.TotalMilliseconds + "");
            // sw.Write("Inverso MergeSort = " + total.TotalMilliseconds + "\n");
            Console.ReadKey(true);
        }
コード例 #2
0
        /// <summary>
        /// Sort the specified array with the algorithm of index i.
        /// </summary>
        /// <returns>The sort.</returns>
        /// <param name="a">The array of integers.</param>
        /// <param name="i">The index of the algorithm.</param>
        static void sort(int[] a, int i)
        {
            switch (i)
            {
            // Bubble Sort.
            case 1:
                // Stores the memory allocated at the moment.
                var mem1 = GC.GetTotalMemory(false);

                // Starts a new stopwatch.
                var watch = System.Diagnostics.Stopwatch.StartNew();

                // Calls the Bubble Sort algorithm.
                BubbleSort.sort(a);

                // Stops the stopwatch.
                watch.Stop();

                // Gets the memory allocated at the moment and sumtracts the
                // older one to get the memory used in the intermediate process.
                var mem2 = GC.GetTotalMemory(false) - mem1;

                // Writes the memory usage in the index of its algorithm in the memory array.
                MainClass.mem[i] = mem2;

                // Gets the value of the stopwatch and stores itin the running time array.
                double time = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            // Similarly the other cases.
            case 2:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                InsertSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 3:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                QuickSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 4:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                HeapSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 5:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                MergeSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;
            }
        }