예제 #1
0
        /// <summary>
        /// Getting type.
        /// </summary>
        /// <param name="intput elem"></param>
        /// <returns></returns>
        public Sorting SortAlg(char s)
        {
            int     num = Convert.ToInt32(s - '0');
            Sorting ans = new InsertionSort();

            if (num == 1)
            {
                ans = new InsertionSort();
            }
            if (num == 2)
            {
                ans = new BubbleSort();
            }
            if (num == 3)
            {
                ans = new QuickSort();
            }
            if (num == 4)
            {
                ans = new HeapSort();
            }
            if (num == 5)
            {
                ans = new MergeSort();
            }
            return(ans);
        }
예제 #2
0
        static void Main(string[] args)
        {
            InsertionSort insertionSort = new InsertionSort();
            BubbleSort    bubbleSort    = new BubbleSort();
            QuickSort     quickSort     = new QuickSort();
            HeapSort      heapSort      = new HeapSort();
            MergeSort     mergeSort     = new MergeSort();

            int[] arr            = StartInput();
            int[] sortedArray    = null;
            int[] alNumbersArray = Utilities.Input();
            for (int i = 0; i < alNumbersArray.Length; i++)
            {
                switch (alNumbersArray[i])
                {
                case 1: sortedArray = insertionSort.Sort(CopyArray(arr)); break;

                case 2: sortedArray = bubbleSort.Sort(CopyArray(arr)); break;

                case 3: sortedArray = quickSort.Sort(CopyArray(arr)); break;

                case 4: sortedArray = heapSort.Sort(CopyArray(arr)); break;

                case 5: sortedArray = mergeSort.Sort(CopyArray(arr)); break;

                default: break;
                }
            }
            Algorithm[] alArr = { insertionSort, bubbleSort, quickSort, heapSort, mergeSort };
            FinalOutput(alArr);
            Console.ReadLine();
        }
예제 #3
0
        public static int[] SwithCase(int[] array, int current, int intTemp)
        {
            int[] array1 = new int[0];
            switch (current)
            {
            case 1:
                InsertionSort insertionSort = new InsertionSort();
                array1 = insertionSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Insertion Sort time" + insertionSort.time);
                break;

            case 2:
                BubbleSort bubbleSort = new BubbleSort();
                array1 = bubbleSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Bubble Sort time" + bubbleSort.time);
                break;

            case 3:
                QuickSort quickSort = new QuickSort();
                array1 = quickSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Quick Sort time" + quickSort.time);
                break;

            case 4:

                HeapSort heapSort = new HeapSort();
                array1 = heapSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Heap Sort time" + heapSort.time);
                break;

            case 5:
                MergeSort mergeSort = new MergeSort();
                mergeSort.Sort(array);
                break;

            default:
                break;
            }
            return(array1);
        }
예제 #4
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);
        }
예제 #5
0
        static void Main(string[] args)
        {
            int[]             sortArray      = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            SortingAlgorithms sortArrayClass = new SortingAlgorithms();

            Console.WriteLine("Array without Sorting:");
            sortArrayClass.printArray(sortArray);
            Console.WriteLine();

            Console.WriteLine("Bubble Sort:");
            sortArrayClass = new BubbleSort();
            sortArrayClass.bubbleSort(sortArray);
            sortArrayClass.printArray(sortArray);
            Console.WriteLine();

            Console.WriteLine("Selection Sort:");
            int[] SelectionsortArray = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            sortArrayClass = new SelectionSort();
            sortArrayClass.selectionSort(SelectionsortArray);
            sortArrayClass.printArray(SelectionsortArray);
            Console.WriteLine();

            Console.WriteLine("Merge Sort:");
            int[] MergesortArray = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            sortArrayClass = new MergeSort();
            sortArrayClass.mergeSort(MergesortArray, 0, MergesortArray.Length - 1);
            sortArrayClass.printArray(MergesortArray);
            Console.WriteLine();


            Console.WriteLine("Insertion Sort:");
            int[] InsertionArray = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            sortArrayClass = new InsertionSort();
            sortArrayClass.insertionSort(InsertionArray);
            sortArrayClass.printArray(InsertionArray);
            Console.WriteLine();



            Console.WriteLine("Quick Sort:");
            int[] QuickArray = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            sortArrayClass = new QuickSort();
            sortArrayClass.quickSort(QuickArray, 0, QuickArray.Length - 1);
            sortArrayClass.printArray(QuickArray);
            Console.WriteLine();


            Console.WriteLine("Heap Sort:");
            int[] HeapArray = { 30, 40, 10, 87, 90, 5, 25, 67, 100, 3, 15 };
            sortArrayClass = new HeapSort();
            sortArrayClass.heapSort(HeapArray);
            sortArrayClass.printArray(HeapArray);
            Console.WriteLine();

            Console.ReadLine();
        }
예제 #6
0
        static void Main(string[] args)
        {
            ConsoleView.Intro(HeapSort.Name);

            int[] rawArray = ConsoleView.GetIntArray();

            HeapSort heapSort = new HeapSort(rawArray);

            ConsoleView.PrintIntArray(heapSort.Sort());

            ConsoleView.End();
        }
예제 #7
0
        static void Test()
        {
            Console.Write("Count: ");
            var count       = int.Parse(Console.ReadLine());
            var sourceArray = Generate(count);
            var sort        = new HeapSort();

            ShowSource(sourceArray);
            var sortResult = sort.Sort(sourceArray);

            ShowResult(sortResult);
            Console.ReadKey();
        }
예제 #8
0
        static void Main(string[] args)
        {
            // Pasirinkti, kas kiek dides musu duomenu kiekis
            Console.WriteLine("Select increment size");
            var input = Console.ReadLine();
            int size  = int.Parse(input);

            // Irasyti i output faila headerius
            File.AppendAllText(Directory.GetCurrentDirectory() + "\\" + "test_results.csv", "Count,Selection,Heap\n");


            for (int i = 0; i <= 100000; i += size)
            {
                if (i != 0)
                {
                    // Inicializuoti klase, kuri skaiciuos algoritmu vykdymu laikus
                    var t = new Stopwatch();

                    // Sugeneruoti duomenis
                    // GenerateSortedData(i) - best-case scenario (bus sugeneruoti jau surikiuoti duomenys)
                    // GenerateData(i) - bus sugeneruoti bet kokie int skaiciai
                    int[] data = GenerateSortedData(i);

                    // Inicializuoti SelectionSort klase ir rikiuoti duomenis
                    var s = new SelectionSort();
                    t.Start();
                    int[] selectSort = s.Sort(data);
                    t.Stop();
                    var selectTime = t.ElapsedMilliseconds;

                    //Atstatyti timeri
                    t.Reset();

                    //Inicializuoti HeapSort klase ir rikiuoti duomenis
                    var h = new HeapSort();
                    t.Start();
                    int[] heap = h.Sort(data);
                    t.Stop();
                    var heapTime = t.ElapsedMilliseconds;

                    // Isvesti i konsole, kiek procentu uzduoties atlikta
                    Console.WriteLine("Progress: " + (float)i / 100000 * 100 + "%");

                    // Isvesti rezultutatus i output faila
                    File.AppendAllText(Directory.GetCurrentDirectory() + "\\" + "test_results.csv", i + "," + selectTime + "," + heapTime + "\n");
                }
            }
        }
예제 #9
0
        static void Tests()
        {
            while (run)
            {
                int[] array = new int[length];
                for (int i = 0; i < length; i++)
                {
                    array[i] = random.Next(10000000);
                }
                //for (int i = 0; i < length; i++)
                //    array[i] = length - i;
                List <int> list = new List <int>(array);

                Test(QuickSort.Sort, array);
                Test(QuickSort.Sort2, array);
                Test(QuickSort.Sort3, array);
                Test(QuickSort.Sort4, array);
                Test(QuickSort.Sort5, array);
                Test(BubbleSort.Sort_InPlace, array);
                Test(BubbleSort.Sort_InPlace2, array);
                Test(InsertionSort.Sort_InPlace, array);
                TestHeap(array);
                Test(HeapSort.Sort, array);
                Test(Array.Sort, array);
                Test(list);//list.Sort

                for (int i = 0; i < length; i++)
                {
                    array[i] = random.Next(10000000);
                }
                HeapSort.Sort(array);
                for (int i = 0; i < array.Length - 1; i++)
                {
                    if (array[i] > array[i + 1])
                    {
                        Console.WriteLine("error: {0} not smaller than {1}", array[i], array[i + 1]);
                    }
                }



                if (Console.ReadKey().Key != ConsoleKey.Spacebar)
                {
                    run = false;
                }
                Console.Clear();
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            var rnd     = new Random();
            var arrSize = rnd.Next(1, 1000);

            int[] arr = Enumerable.Repeat(0, arrSize).Select(x => rnd.Next(int.MinValue, int.MaxValue)).ToArray();
            var   bs  = (int[])arr.Clone();

            BubbleSort.Sort(bs);
            Assert.IsTrue(IsSorted(bs));

            var ss = (int[])arr.Clone();

            SelectionSort.Sort(ss);
            Assert.IsTrue(IsSorted(ss));

            var @is = (int[])arr.Clone();

            InsertionSort.Sort(@is);
            Assert.IsTrue(IsSorted(@is));

            var ms = (int[])arr.Clone();

            MergeSort.Sort(ms);
            Assert.IsTrue(IsSorted(ms));

            var qs = (int[])arr.Clone();

            QuickSort.Sort(qs);
            Assert.IsTrue(IsSorted(qs));

            var cs = (int[])arr.Clone();

            CountingSort.Sort(cs);
            Assert.IsTrue(IsSorted(cs));

            var rs = (int[])arr.Clone();

            RadixSort.Sort(rs);
            Assert.IsTrue(IsSorted(rs));

            var hs = (int[])arr.Clone();

            HeapSort.Sort(hs);
            Assert.IsTrue(IsSorted(hs));
        }
예제 #11
0
        static void Main(string[] args)
        {
            RegisterDependencies();
            int[] unsortedNumbers;

            using (var scope = Container.BeginLifetimeScope())
            {
                _timerService = scope.Resolve <ITimerService>();

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var heapSort = new HeapSort(_timerService);
                Sort(heapSort, unsortedNumbers);

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var mergeSort = new MergeSort(_timerService);
                Sort(mergeSort, unsortedNumbers);

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var quickSort = new QuickSort(_timerService);
                Sort(quickSort, unsortedNumbers);
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            //Generate random 1D array

            int[] myArray     = ArrayGenerator();
            int[] myTempArray = new int[myArray.Length];
            Array.Copy(myArray, myTempArray, myArray.Length);

            //Print the unsorted list to the console so it can be checked

            Console.WriteLine("Unsorted list:");

            foreach (int element in myArray)
            {
                Console.WriteLine(element);
            }

            Console.WriteLine();

            //Calling different sorting algorithms on the list and putting the results into TXTs

            Console.WriteLine("Check on array that has duplicates:");
            Console.WriteLine();

            BubbleSort.BubbleSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            SelectionSort.SelectionSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //MergeSort.MergeSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            InsertionSort.InsertionSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            BucketSort.BucketSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            QuickSort.QuickSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            GnomeSort.GnomeSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CombSort.CombSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CountingSort.CountingSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            HeapSort.HeapSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //CycleSort.CycleSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //BogoSort.BogoSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CocktailSort.CocktailSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            StoogeSort.StoogeSortAlgorithm(new int[] { 2, 4, 5, 3, 1 });
            myTempArray.CopyTo(myArray, 0);

            OddEvenSort.OddEvenSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            BubbleSortRecursive.BubblesortRecursiveAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //InsertionSortRecursive.InsertionSortRecursiveAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //ShellSort.ShellSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            StrandSort.StrandSortAlgorithm(new int[] { 3, 5, 5, 1, 5, 3, 10, 8, 9 });
            myTempArray.CopyTo(myArray, 0);

            //

            Console.ReadKey();
        }
예제 #13
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Console.WriteLine("Enter the Number");

            int c = Console.Read();

            int[] arr = new int[c];

            for (int i = 0; i < c; i++)
            {
                arr[i] = random.Next();
            }


            Console.WriteLine("Choose The Algorhitm ");

            Console.WriteLine("1: InsertSort");

            Console.WriteLine("2: BubbleSort");

            Console.WriteLine("3: Quick Sort");

            Console.WriteLine("4: Heap Sort");

            Console.WriteLine("5: Merge Sort");

            Console.WriteLine("6: All");



            string s = Console.ReadLine();

            if (s.Length > 1)
            {
                for (int i = Convert.ToInt32(s.Substring(0, 1)); i <= Convert.ToInt32(s.Substring(2, 3)); i++)
                {
                    switch (Convert.ToString(i))
                    {
                    case "1":
                        InsertSort.Sort(arr);
                        break;

                    case "2":
                        BubbleSort.Sort(arr);
                        break;

                    case "3":
                        QuickSort.Sort(arr, 0, arr.Length - 1);
                        break;

                    case "4":
                        HeapSort.Sort(ref arr);
                        break;

                    case "5":
                        MergeSort.Sort(ref arr, 0, arr.Length - 1);
                        break;
                    }
                }
            }
            else
            {
                switch (s)
                {
                case "1":
                    InsertSort.Sort(arr);
                    break;

                case "2":
                    BubbleSort.Sort(arr);
                    break;

                case "3":
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    break;

                case "4":
                    HeapSort.Sort(ref arr);
                    break;

                case "5":
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;

                case "6":
                    InsertSort.Sort(arr);
                    BubbleSort.Sort(arr);
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    HeapSort.Sort(ref arr);
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;
                }
            }

            Console.ReadLine();
        }
예제 #14
0
        static void Main(string[] args)
        {
            int size  = 20000;
            int range = 10000;

            BubbleSort bubbleSort = new BubbleSort();

            int[] unsortedArray1 = Utils.generateArray(size, range);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            bubbleSort.bubbleSort(unsortedArray1);
            watch.Stop();
            var elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("bubble took time: " + elapsedMs);

            CountSort countSort = new CountSort();

            int[] unsortedArray2 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            countSort.countSort(unsortedArray2);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("count took time: " + elapsedMs);

            MergeSort mergeSort = new MergeSort();

            int[] unsortedArray3 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            MergeSort.sort(unsortedArray3, 0, unsortedArray3.Length - 1);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("merge took time: " + elapsedMs);

            InsertionSort insertionSort = new InsertionSort();

            int[] unsortedArray4 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            insertionSort.insertionSort(unsortedArray4);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("insertion took time: " + elapsedMs);

            QuickSort quickSort = new QuickSort();

            int[] unsortedArray5 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            quickSort.quickSort(unsortedArray5);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("quick took time: " + elapsedMs);


            HeapSort heapSort = new HeapSort();

            int[] unsortedArray6 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            heapSort.heapSort(unsortedArray6);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("heap took time: " + elapsedMs);

            SelectionSort selectionSort = new SelectionSort();

            int[] unsortedArray7 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            selectionSort.selectionSort(unsortedArray7);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("selection took time: " + elapsedMs);

            Console.ReadKey();
        }
예제 #15
0
파일: Tester.cs 프로젝트: mtyylx/Basics
        static void Main(string[] args)
        {
            int arraySize = 10;
            int arrayUpperLimits = 100;
            int repeatTimes = 1;
            bool enableLogging = true;

            int[] inputArray;
            double[] executionTime = new double[repeatTimes]; 
            Stopwatch stopwatch = new Stopwatch();

            //Slow - O(n^2)
            InsertionSort mInsertionSort = new InsertionSort();
            SelectionSort mSelectionSort = new SelectionSort();
            BubbleSort mBubbleSort = new BubbleSort();

            //Fast - O(n*lgn)
            MergeSort mMergeSort = new MergeSort();
            HeapSort mHeapSort = new HeapSort();
            QuickSort mQuickSort = new QuickSort();
           
            //Linear - O(n)
            CountingSort mCountingSort = new CountingSort();
            RadixSort mRadixSort = new RadixSort();
            BucketSort mBucketSort = new BucketSort();

            //Test the execution time repeatly.
            for (int i = 0; i < repeatTimes; i++)
            {
                //Always create new Array object, even though the contents are the same.
                //Because each array will be modified at the end of each loop.
                inputArray = ArrayHandler.Create(arraySize, enableLogging, arrayUpperLimits);
                //inputArray = ArrayHandler.CreateAlmostSorted(arraySize, enableLogging);
                //inputArray = new int[] { 122, 44, 122, 55, 33, 55, 44, 23};
                stopwatch.Start();
                //----------------------------------------------------------------------------------------//
                //             A L G O R I T H M         T E S T E D        H E R E                       //
                //----------------------------------------------------------------------------------------//

                //------- 0.Sort in VC# -------
                //Array.Sort(inputArray);                                                         //7ms 10^5

                //#####################################  O(n*n)  #############################################

                //------- 1.Insertion Sort ~ O(n^2) -------
                mInsertionSort.Sort(inputArray);                                                //95ms 10^4
                //mInsertionSort.SortWithTrace(inputArray);
                //mInsertionSort.Sort_Recursive(inputArray);

                //------- 2.Selection Sort ~ O(n^2) -------
                //mSelectionSort.Sort(inputArray);                                                //164ms 10^4
                //mSelectionSort.SortWithTrace(inputArray);

                //------- 3.Bubble Sort ~ O(n^2) -------
                //mBubbleSort.Sort(inputArray);                                                   //600ms 10^4
                //mBubbleSort.OriginalBubbleSort(inputArray);                                     //550ms 10^4

                //###################################  O(n*lgn)  #############################################

                //------- 4.Merge Sort ~ O(n*lgn) -------
                //mMergeSort.Sort(inputArray);                                                    //27ms 10^5
                //mMergeSort.Sort_Enhanced(inputArray);                                           //25ms 10^5

                //------- 5.Heap Sort ~ O(n*lgn) -------
                //mHeapSort.Sort(inputArray);                                                     //53ms 10^5

                //------- 6.Quick Sort ~ O(n*lgn) -------
                //mQuickSort.Sort(inputArray);                                                      //40ms 10^5
                //mQuickSort.Sort_Hoare(inputArray, enableLogging);                               //23ms 10^5
                //mQuickSort.Sort_Lomuto(inputArray, enableLogging);

                //######################################  O(n)  ##############################################

                //------- 7.Counting Sort ~ O(n) -------
                //inputArray = mCountingSort.Sort(inputArray);                                    //2ms 10^5

                //------- 8.Radix Sort ~ O(n) -------
                //inputArray = mRadixSort.Sort(inputArray, enableLogging);                        //114ms 10^5

                //------- 9.Bucket Sort ~ O(n) -------
                //inputArray = mBucketSort.Sort(inputArray);                                      //13ms 10^5

                //------------------------------------------------------------------------------------------
                //             A L G O R I T H M         T E S T        E N D E D
                //------------------------------------------------------------------------------------------
                stopwatch.Stop();
                executionTime[i] = stopwatch.ElapsedMilliseconds;
                Console.Write(executionTime[i] + " ");
                Console.WriteLine("");
                stopwatch.Reset();
                ArrayHandler.Print(inputArray, enableLogging);
            }

            //Print Execution Time
            double ts = executionTime.Average();
            Console.WriteLine("Average Execution Time in milliseconds: " + ts);

            Console.ReadLine();
        }
예제 #16
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;
            }
        }