Ejemplo n.º 1
0
        /// <summary>
        /// Sorts arrays and returns min time
        /// </summary>
        /// <param name="randomArray">Array of integers</param>
        /// <param name="algorithms">Selected algorithms</param>
        /// <returns></returns>
        private static double SortAndGetMinTime(int[] randomArray, int[] algorithms)
        {
            int[] copyArray = new int[randomArray.Length];

            double min = double.MaxValue;

            for (int i = 0; i < algorithms.Length; i++)
            {
                switch (algorithms[i])
                {
                case 1:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    InsertionSort.Sort(copyArray);
                    if (InsertionSort.GetTime() < min)
                    {
                        min = InsertionSort.GetTime();
                    }
                    break;

                case 2:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    BubbleSort.Sort(copyArray);
                    if (BubbleSort.GetTime() < min)
                    {
                        min = BubbleSort.GetTime();
                    }
                    break;

                case 3:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    QuickSort.Sort(copyArray);
                    if (QuickSort.GetTime() < min)
                    {
                        min = QuickSort.GetTime();
                    }
                    break;

                case 4:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    HeapSort.Sort(copyArray);
                    if (HeapSort.GetTime() < min)
                    {
                        min = HeapSort.GetTime();
                    }
                    break;

                case 5:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    MergeSort.Sort(copyArray);
                    if (MergeSort.GetTime() < min)
                    {
                        min = MergeSort.GetTime();
                    }
                    break;
                }
            }
            return(min);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prints result of sort algorithms used memory and time
        /// </summary>
        /// <param name="algorithms">Array of integers</param>
        /// <param name="minTime"></param>
        private static void PrintResult(int[] algorithms, double minTime)
        {
            for (int i = 0; i < algorithms.Length; i++)
            {
                switch (algorithms[i])
                {
                case 1:
                    if (InsertionSort.GetTime() == minTime)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    Console.WriteLine("Insertion Sort Agorithm");
                    Console.WriteLine("Running time: {0} miliseconds", InsertionSort.GetTime());
                    Console.WriteLine("Memory usage: {0} bytes", InsertionSort.GetMemory());
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case 2:
                    if (BubbleSort.GetTime() == minTime)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    Console.WriteLine("Bubble Sort Agorithm");
                    Console.WriteLine("Running time: {0} miliseconds", BubbleSort.GetTime());
                    Console.WriteLine("Memory usage: {0} bytes", BubbleSort.GetMemory());
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case 3:
                    if (QuickSort.GetTime() == minTime)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    Console.WriteLine("Quick Sort Agorithm");
                    Console.WriteLine("Running time: {0} miliseconds", QuickSort.GetTime());
                    Console.WriteLine("Memory usage: {0} bytes", QuickSort.GetMemory());
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case 4:
                    if (HeapSort.GetTime() == minTime)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    Console.WriteLine("Heap Sort Agorithm");
                    Console.WriteLine("Running time: {0} miliseconds", HeapSort.GetTime());
                    Console.WriteLine("Memory usage: {0} bytes", HeapSort.GetMemory());
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case 5:
                    if (MergeSort.GetTime() == minTime)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    Console.WriteLine("Merge Sort Agorithm");
                    Console.WriteLine("Running time: {0} miliseconds", MergeSort.GetTime());
                    Console.WriteLine("Memory usage: {0} bytes", MergeSort.GetMemory());
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                }
            }
        }