Beispiel #1
0
        static void Main(string[] args)
        {
            int[] array = new int[] { 5, 10, 3, 2, 4, 3 };
            Console.Write("Bubble Sort: ");
            BubbleSort.sort(array);
            print(array);

            array = new int[] { 5, 10, 3, 2, 4, 3 };
            Console.Write("Insertion Sort: ");
            InsertionSort.sort(array);
            print(array);

            array = new int[] { 5, 10, 3, 2, 4, 3 };
            Console.Write("Selection Sort: ");
            SelectionSort.sort(array);
            print(array);

            array = new int[] { 5, 10, 3, 2, 4, 3 };
            Console.Write("Merge Sort: ");
            MergeSort.sort(array, 0, array.Length - 1);
            print(array);
        }
Beispiel #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;
            }
        }