Exemple #1
0
        static void Main(string[] args)
        {
            int[] a = { 123, 543, 654, 34, 645, 75, 234, 98, 23, 1, 5658, 980, 56, 231, 6, 35, 8, 923, 68, 34 };

            int[] c = MergeSort.Sort(a);
            c = SelectionSort.Sort(a, true);
            c = InsertionSort.Sort(a, true);
            c = BubbleSort.Sort(a, true);
        }
Exemple #2
0
        private void btnSort_Click(object sender, EventArgs e)
        {
            int[]      dizi = { 5, 1, 12, -5, 16 };
            BubbleSort bs   = new BubbleSort();

            txtOnce.Text = bs.PrintItems(dizi);
            bs.Sort(dizi);
            txtSonra.Text = bs.PrintItems(dizi);
        }
        static void BubbleTest()
        {
            int[] arr = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            BubbleSort.Sort(arr);
            Console.WriteLine(string.Join(" | ", arr));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            BubbleSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
        private static int[] RunBubbleSort(int arraySize, int[] bigArray)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            var bs    = BubbleSort.Sort((int[])bigArray.Clone());

            watch.Stop();
            Console.WriteLine($"BubbleSort {arraySize} elements = {watch.ElapsedMilliseconds} ms");

            return(bs);
        }
        static void Main(string[] args)
        {
            BubbleSort bs = new BubbleSort();
            QuickSort  qs = new QuickSort();

            int[] arr = new int[] { 12, 23, 23, 7, 45, 29 };

            print(arr, "Array(unsorted)");
            print(bs.Sort(arr), "BubbleSort");
            print(qs.Sort(arr), "QuickSort");

            Console.ReadKey();
        }
Exemple #6
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
        }
Exemple #7
0
        static void Main2(string[] args)
        {
            Bicycle[] bicycles = new Bicycle[3];
            bicycles[0] = new Bicycle("Steven's Bicycles", System.Drawing.Color.Green, "This is a feisty bike with a bit of zest", 4.9);
            bicycles[1] = new Bicycle("bike two", System.Drawing.Color.Red, "This is a cute bike", 3.2);
            bicycles[2] = new Bicycle("Another bike", System.Drawing.Color.Turquoise, "Bikey McBike Face", 13.8);
            BubbleSort.Sort(bicycles);
            Bicycle.PrintBicycles(bicycles);


            int[] integers = new int[] { 1, 2, 3, 18, 32, 55, 34, 89, 15, 23, 41, 6, 18, 23 };
            //int[] integers = new int[] { 4, 18, 11, 7, 13, 6, 3, 1, 9 };
            BubbleSort.Sort(integers);
            //MergeSort.InArraySort(integers);
            Console.WriteLine(BinarySearch.Search(integers, 89));
            //SelectionSort.Sort(integers);
            foreach (int i in integers)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            string[] strings = new string[] { "als", "ik", "boven", "op", "de", "dom", "sta", "kijk", "ik" };
            MergeSort.Sort(strings);
            //SelectionSort.Sort(strings);
            foreach (string s in strings)
            {
                Console.Write(s + " ");
            }
            Console.WriteLine();

            int[] AddedNumber = BinaryAddition.Add(new int[] { 1, 0, 0, 1, 0, 1 }, new int[] { 1, 1, 0, 0, 1, 1 });
            foreach (int i in AddedNumber)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            LinearSearch ls = new LinearSearch();

            ls.Search();

            InsertionSort.InsertionSortConsoleProgram();
        }
Exemple #8
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);
        }
Exemple #9
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();
        }
Exemple #10
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;
                }
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            int choice, i, n;

            int[] a = new int[20];

            while (true)
            {
                WriteLine();
                Write("Enter the total number of elements in the array: ");
                n = Convert.ToInt32(ReadLine());

                for (i = 0; i < n; i++)
                {
                    Write($"Enter element {(i + 1)}: ");
                    a[i] = Convert.ToInt32(ReadLine());
                }

                WriteLine();
                WriteLine("Array is: ");
                for (i = 0; i < n; i++)
                {
                    Write($"{a[i]} ");
                }
                WriteLine();

                WriteLine();
                WriteLine("Would you like to: ");

                WriteLine("1. Bubble Sort");
                WriteLine("2. Quick Sort");
                WriteLine("3. Quit");
                WriteLine();

                Write("Please enter your selection: ");
                choice = Convert.ToInt32(ReadLine());
                WriteLine();

                if (choice == 3)
                {
                    break;
                }

                switch (choice)
                {
                case 1:
                    BubbleSort.Sort(a, n);
                    WriteLine("Sorted Array is: ");
                    for (i = 0; i < n; i++)
                    {
                        Write($"{a[i]} ");
                    }
                    WriteLine();
                    break;

                case 2:
                    QuickSort.Sort(a, n);
                    WriteLine("Sorted Array is: ");
                    for (i = 0; i < n; i++)
                    {
                        Write($"{a[i]} ");
                    }
                    WriteLine();
                    break;

                default:
                    WriteLine("Please enter a valid selection.");
                    break;
                }
            }
        }