Exemple #1
0
        static void Main(string[] args)
        {
            int[]     arr       = new int[] { 1, 5, 6, 8, 13, 45, 65, 121, 123, 163, 245, 334 };
            QuickSort quickSort = new QuickSort();

            var sortedArray = quickSort.DoQuickSort(arr);

            foreach (int element in sortedArray)
            {
                Console.Write(element + " ");
            }
            Console.WriteLine("");
        }
Exemple #2
0
        static void PerformQuickSort(int sizeArray)
        {
            SortingAlgorithms sorting = new QuickSort(sizeArray);

            sorting.FillArray();
            Stopwatch watch = new Stopwatch();

            watch.Start();
            sorting.SortArray();
            watch.Stop();
            var arr = sorting.GetSortedArray;

            Print(arr);
            Console.WriteLine($"Total Execution Time: {watch.ElapsedMilliseconds} ms");
        }
Exemple #3
0
        static void Main(string[] args)
        {
            int[] bubble = { 4, 8, 1, 11, 5, 3, 2, 10 };
            Sort.BubbbleSort(bubble);

            foreach (var item in bubble)
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();

            int[] selection = { 4, 8, 1, 11, 5, 3, 2, 10 };
            Sort.SelectionSort(selection);

            foreach (var item in selection)
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();

            int[] insert = { 4, 8, 1, 11, 5, 3, 2, 10 };
            Sort.InsertSort(insert);

            foreach (var item in insert)
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();

            int[] merge = { 4, 8, 1, 11, 5, 3, 2, 10 };
            MergeSort.Sort(merge, 0, merge.Length - 1);

            foreach (var item in merge)
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();

            int[] quick = { 8, 8, 8, 4, 11, 9, 3 };
            QuickSort.Sort(quick, 0, quick.Length - 1);

            foreach (var item in quick)
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();
        }
Exemple #4
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);
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            QuickSort qSorter = new QuickSort();

            int[]  test   = { 3, 4, 1, 10, 50, 5, 100, -3, 0 };
            string before = "";

            for (int i = 0; i < test.Length; i++)
            {
                before += test[i] + " ";
            }
            Console.WriteLine("before: {0}", before);
            qSorter.quickSort(test, 0, test.Length - 1);
            string after = "";

            for (int i = 0; i < test.Length; i++)
            {
                after += test[i] + " ";
            }
            Console.WriteLine("after: {0}", after);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            int counter = 100;
            List<IComparable> values = new List<IComparable>();

            var random = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < counter; i++)
            {
                values.Add(new UserType(random.Next(0, 1000)));
            }

            var bubbleSort = new BubbleSort<IComparable, IList<IComparable>>();
            RunAlgorithm(values, bubbleSort);

            var shelllSort = new ShellSort<IComparable, IList<IComparable>>();
            RunAlgorithm(values, shelllSort);

            var quickSort = new QuickSort<IComparable, IList<IComparable>>();
            RunAlgorithm(values, quickSort);

            Console.ReadKey();
        }
Exemple #7
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 #8
0
        static void RunQuickSort()
        {
            Console.WriteLine("Quicksort");
            Char[] arr = chars.ToArray();
            OutputCharArray(arr, "Input");
            QuickSort<Char> qs = new QuickSort<char>();
            qs.Sort(ref arr, 0, arr.Length - 1);
            OutputCharArray(arr, "Output");

            Int64[] arri = ints.ToArray();
            OutputIntArray(arri, "Input");
            QuickSort<Int64> qsi = new QuickSort<Int64>();
            qsi.Sort(ref arri, 0, arri.Length - 1);
            OutputIntArray(arri, "Output");
        }
Exemple #9
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;
                }
            }
        }