Esempio n. 1
0
        static void Main(string[] args)
        {
            var array = new int[] { 5, 4, 3 };

            Console.WriteLine("Array: " + string.Join(' ', array));

            #region InsertionSort

            var stopWatch = new Stopwatch();
            stopWatch.Start();
            var insertionSortResult = InsertionSort.Sort(array);
            stopWatch.Stop();
            var ts = stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            var elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";
            Console.WriteLine("Insertion Sort RunTime " + elapsedTime);
            Console.WriteLine("Insertion Sort Result " + string.Join(' ', insertionSortResult));

            #endregion

            #region BubbleSort

            stopWatch = new Stopwatch();
            stopWatch.Start();
            var bubbleSortResult = BubbleSort.Sort(array);
            stopWatch.Stop();
            ts = stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";
            Console.WriteLine("Bubble Sort RunTime " + elapsedTime);
            Console.WriteLine("Bubble Sort Result " + string.Join(' ', insertionSortResult));

            #endregion

            #region BubbleSort

            stopWatch = new Stopwatch();
            stopWatch.Start();
            var shellSortResult = ShellSort.Sort(array);
            stopWatch.Stop();
            ts = stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";
            Console.WriteLine("Shell Sort RunTime " + elapsedTime);
            Console.WriteLine("Shell Sort Result " + string.Join(' ', shellSortResult));

            #endregion
        }
Esempio n. 2
0
        private static void SolveWithInsertionSort()
        {
            int    length = 100;
            string test   = "100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1";

            int[]    testCase  = new int[length];
            string[] testInStr = test.Split(' ');
            for (int i = 0; i < length; i++)
            {
                testCase[i] = int.Parse(testInStr[i]);
            }
            ISort sort = new InsertionSort();

            int[] result = sort.Sort(testCase);
            Display(result);
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Insertion sort");
            var array = new int[] { 6, 5, 3, 1, 8, 7, 2, 4, 6, 6 };

            InsertionSort.Sort(array);

            Console.WriteLine("Quick sort");
            array = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
            QuickSort.Sort(array, 0, array.Length - 1);
            Print(array);

            Console.WriteLine("Merge sort");
            array = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
            var result = MergeSort.Sort(array);

            Print(result);
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Various sorting techniques:");
            var array = new int[] { 7, 9, 2, 8, 3, 4 };

            Console.WriteLine("Bubble sort");
            BubbleSort.Sort(array);
            Print(array);

            array = new int[] { 7, 9, 2, 8, 3, 4 };
            Console.WriteLine("Selection sort");
            SelectionSort.Sort(array);
            Print(array);

            array = new int[] { 7, 9, 2, 8, 3, 4 };
            Console.WriteLine("Insertion sort");
            InsertionSort.Sort(array);
            Print(array);
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Sorting Algorithms ");


            //Selection Sort
            int[] A = { 2, 7, 4, 1, 5, 3 };
            Console.WriteLine("Actual Array: ");
            A.ToList().ForEach(i => Console.Write(i.ToString() + " "));
            //Console.WriteLine();
            var selection        = new SelectionSort();
            var selectionSortedA = selection.Sort(A);

            Console.WriteLine("\nSelection sorted Array: ");
            selectionSortedA.ToList().ForEach(i => Console.Write(i.ToString() + " "));

            //Bubble Sort
            int[] B = { 2, 7, 4, 1, 5, 3 };
            Console.WriteLine("\n\nActual Array: ");
            B.ToList().ForEach(i => Console.Write(i.ToString() + " "));
            var bubble        = new BubbleSort();
            var bubbleSortedB = bubble.Sort(B);

            Console.WriteLine("\nBubble sorted Array: ");
            bubbleSortedB.ToList().ForEach(i => Console.Write(i.ToString() + " "));

            //Insertion Sort
            int[] C = { 2, 7, 4, 1, 5, 3 };
            Console.WriteLine("\n\nActual Array: ");
            C.ToList().ForEach(i => Console.Write(i.ToString() + " "));
            var insertion        = new InsertionSort();
            var insertionSortedC = insertion.Sort(C);

            Console.WriteLine("\nInsertion sorted Array: ");
            insertionSortedC.ToList().ForEach(i => Console.Write(i.ToString() + " "));
            Console.ReadLine();
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.Write("Enter size : ");
            int size = int.Parse(Console.ReadLine());

            // Generate array with random elements
            int[] arr = Generator(size);

            // Array for sorting
            int[] changedArr = new int[arr.Length];

            // Array for calculating time
            TimeSpan[] delta = new TimeSpan[5];

            Print(arr);

            Console.Write("Select which algorithm you want to perform :\n1-Insertion sort\n" +
                          "2-Bubble sort\n3-Quicksort\n4-Heap sort\n5-Merge sort\n6-All\nEnter number(s) : ");

            string n = Console.ReadLine();

            string[] num;

            // Exploration of input string
            if (n.Contains(','))
            {
                num = n.Split(',');
            }
            else if (n.Contains('-'))
            {
                num = new string[int.Parse((n[n.Length - 1]).ToString()) - int.Parse(n[0].ToString()) + 1];
                int t = 0;
                for (int i = int.Parse(n[0].ToString()); i <= int.Parse((n[n.Length - 1]).ToString()); i++)
                {
                    num[t++] = i.ToString();
                }
            }
            else
            {
                num = new string[] { n }
            };

            // Call needed function or functions
            for (int i = 0; i < num.Length; i++)
            {
                switch (num[i])
                {
                case "1": changedArr = InsertionSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + InsertionSort.MemoryAllocation() + " byte"); break;

                case "2": changedArr = BubbleSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + BubbleSort.MemoryAllocation() + " byte"); break;

                case "3": changedArr = QuickSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + QuickSort.MemoryAllocation() + " byte"); break;

                case "4": changedArr = HeapSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + HeapSort.MemoryAllocation() + " byte"); break;

                case "5": changedArr = MergeSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + MergeSort.MemoryAllocation() + " byte"); break;

                case "6":
                    DateTime inT1 = DateTime.Now;
                    changedArr = InsertionSort.Sort(arr); Print(changedArr);
                    DateTime inT2 = DateTime.Now;  delta[0] = inT2 - inT1;
                    Console.WriteLine("Memory = " + InsertionSort.MemoryAllocation() + " byte");

                    DateTime buT1 = DateTime.Now;
                    changedArr = BubbleSort.Sort(arr); Print(changedArr);
                    DateTime buT2 = DateTime.Now;  delta[1] = buT2 - buT1;
                    Console.WriteLine("Memory = " + BubbleSort.MemoryAllocation() + " byte");

                    DateTime quT1 = DateTime.Now;
                    changedArr = QuickSort.Sort(arr); Print(changedArr);
                    DateTime quT2 = DateTime.Now;  delta[2] = quT2 - quT1;
                    Console.WriteLine("Memory = " + QuickSort.MemoryAllocation() + " byte");

                    DateTime heT1 = DateTime.Now;
                    changedArr = HeapSort.Sort(arr); Print(changedArr);
                    DateTime heT2 = DateTime.Now;  delta[3] = heT2 - heT1;
                    Console.WriteLine("Memory = " + HeapSort.MemoryAllocation() + " byte");

                    DateTime meT1 = DateTime.Now;
                    changedArr = MergeSort.Sort(arr); Print(changedArr);
                    DateTime meT2 = DateTime.Now;  delta[4] = meT2 - meT1;
                    Console.WriteLine("Memory = " + MergeSort.MemoryAllocation() + " byte");

                    for (int j = 0; j < delta.Length; j++)
                    {
                        if (delta[j] == delta.Min())
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(delta[j]);
                            Console.ResetColor();
                        }
                        else
                        {
                            Console.WriteLine(delta[j]);
                        }
                    }

                    break;

                default: Console.WriteLine("Incorrect number!"); break;
                }
            }
        }