Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the size of an array that you want to sort");
            int size = Convert.ToInt32(Console.ReadLine());

            int[]  unsortedArray = new int[size];
            Random random        = new Random();

            Console.WriteLine("Original array elements:");
            for (int i = 0; i < unsortedArray.Length; i++)
            {
                unsortedArray[i] = random.Next(0, 100);
                Console.Write(unsortedArray[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Please enter the algorithms you want to use to sort your array");

            string algorithms = Console.ReadLine();

            if (algorithms == "All")
            {
                var watch1 = new System.Diagnostics.Stopwatch();
                watch1.Start();
                Sorting bubbleSorting      = new BubbleSorting();
                var     sortedByBubbleSort = bubbleSorting.Sort(unsortedArray);
                watch1.Stop();
                var time1 = watch1.ElapsedMilliseconds;

                bubbleSorting.Print(sortedByBubbleSort);

                var watch2 = new System.Diagnostics.Stopwatch();
                watch2.Start();
                Sorting insertionSorting      = new InsertionSorting();
                var     sortedByInsertionSort = insertionSorting.Sort(unsortedArray);
                watch2.Stop();
                var time2 = watch2.ElapsedMilliseconds;
                insertionSorting.Print(sortedByInsertionSort);

                var watch3 = new System.Diagnostics.Stopwatch();
                watch3.Start();
                Sorting mergeSorting      = new MergeSorting();
                var     sortedByMergeSort = mergeSorting.Sort(unsortedArray);
                watch3.Stop();
                var time3 = watch3.ElapsedMilliseconds;
                mergeSorting.Print(sortedByMergeSort);

                Console.WriteLine($"Execution Time for Bubble Sort: {time1} ms");
                Console.WriteLine($"Execution Time for Insertion Sort: {time2} ms");
                Console.WriteLine($"Execution Time for Merge Sort: {time3} ms");
            }

            string[] algs = algorithms.Split(',');

            foreach (var algorithm in algs)
            {
                switch (algorithm)
                {
                case "1":
                    var watch1 = new System.Diagnostics.Stopwatch();
                    watch1.Start();
                    Sorting bubbleSorting      = new BubbleSorting();
                    var     sortedByBubbleSort = bubbleSorting.Sort(unsortedArray);
                    watch1.Stop();
                    Console.WriteLine($"Execution Time for Bubble Sort: {watch1.ElapsedMilliseconds} ms");
                    bubbleSorting.Print(sortedByBubbleSort);
                    break;

                case "2":
                    var watch2 = new System.Diagnostics.Stopwatch();
                    watch2.Start();
                    Sorting insertionSorting      = new InsertionSorting();
                    var     sortedByInsertionSort = insertionSorting.Sort(unsortedArray);
                    watch2.Stop();
                    Console.WriteLine($"Execution Time for Insertion Sort: {watch2.ElapsedMilliseconds} ms");
                    insertionSorting.Print(sortedByInsertionSort);
                    break;

                case "3":
                    var watch3 = new System.Diagnostics.Stopwatch();
                    watch3.Start();
                    Sorting mergeSorting      = new MergeSorting();
                    var     sortedByMergeSort = mergeSorting.Sort(unsortedArray);
                    watch3.Stop();
                    Console.WriteLine($"Execution Time for Merge Sort: {watch3.ElapsedMilliseconds} ms");
                    mergeSorting.Print(sortedByMergeSort);
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private static void SortWithEveryMethod(List <int> data, System.Diagnostics.Stopwatch watch)
        {
            TimeSpan      ts;
            List <double> time = new List <double>();

            Console.WriteLine("Insertion Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> insSrtAsc = InsertionSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in insSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> insSrtDesc = InsertionSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in insSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nBubble Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> bubSrtAsc = BubbleSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bubSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> bubSrtDesc = BubbleSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bubSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nSelection Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> selSrtAsc = SelectionSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in selSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> selSrtDesc = SelectionSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in selSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nMerge sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> merSrtAsc = MergeSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in merSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> merSrtDesc = MergeSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in merSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nCounting sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> couSrtAsc = CountingSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in couSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> couSrtDesc = CountingSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in couSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nBucket sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> bucSrtAsc = BucketSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bucSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> bucSrtDesc = BucketSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bucSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nQuickSort:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> quiSrtAsc = QuickSort.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in quiSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> quiSrtDesc = QuickSort.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in quiSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nHeapSort:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> heaSrtAsc = HeapSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in heaSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> heaSrtDesc = HeapSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in heaSrtDesc)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.WriteLine("time:");
            for (int i = 0; i < time.Count; i++)
            {
                switch (i)
                {
                case 0:
                    Console.Write("Insertion: ");
                    break;

                case 2:
                    Console.Write("\nBubble: ");
                    break;

                case 4:
                    Console.Write("\nSelection: ");
                    break;

                case 6:
                    Console.Write("\nMerge: ");
                    break;

                case 8:
                    Console.Write("\nCounting: ");
                    break;

                case 10:
                    Console.Write("\nBucket: ");
                    break;

                case 12:
                    Console.Write("\nQuickSort: ");
                    break;

                case 14:
                    Console.Write("\nHeap: ");
                    break;

                default:
                    Console.Write(" ");
                    break;
                }
                Console.Write(time[i]);
            }
        }