Exemple #1
0
        static void Main(string[] args)
        {
            var randArray         = GenerateRandomArray();
            var randArraySorted   = GenerateRandomArray().OrderBy((a) => a).ToArray();
            var randArrayReversed = GenerateRandomArray().OrderByDescending((a) => a).ToArray();


            Console.WriteLine("Selection Sort");
            Display.ExecutionTime(() =>
            {
                SelectionSort.Sort(randArray);
                SelectionSort.Sort(randArraySorted);
                SelectionSort.Sort(randArrayReversed);
            });


            Console.WriteLine("Insertion Sort");

            Display.ExecutionTime(() =>
            {
                InsertionSort.Sort(randArray);
                InsertionSort.Sort(randArraySorted);
                InsertionSort.Sort(randArrayReversed);
            });

            Console.WriteLine("QuickSort");

            Display.ExecutionTime(() =>
            {
                QuickSort.Sort(randArray, 0, randArray.Length - 1);
                QuickSort.Sort(randArraySorted, 0, randArraySorted.Length - 1);
                QuickSort.Sort(randArrayReversed, 0, randArrayReversed.Length - 1);
            });
        }
Exemple #2
0
        static void Main(string[] args)
        {
            int[] set = new[] { 22, 7, 5, 2, 55, 11, 12, 67 };
            BubbleSort.PrintIterations = false;
            MergeSort.PrintIterations  = false;
            QuickSort.PrintIterations  = false;

            //BubbleSort
            Console.WriteLine($"\n\n-----------------BubbleSort------------------\n\n");
            Console.WriteLine($"Original Array: {String.Join(",", set)}");
            Console.WriteLine($"\nSorted Array: {String.Join(",", BubbleSort.Sort(set))}");
            Console.WriteLine("Number of Iterations {0}", BubbleSort.IterationsCounter);
            Console.ReadKey();

            set = new[] { 22, 7, 5, 2, 55, 11, 12, 67 };
            //QuickSort

            Console.WriteLine($"\n\n-----------------MergeSort------------------\n\n");
            Console.WriteLine($"Original Array: {String.Join(",", set)}");
            Console.WriteLine($"\nSorted Array: {String.Join(",", MergeSort.Sort(set))}");
            Console.WriteLine("Number of Iterations {0}", MergeSort.IterationsCounter);
            Console.ReadKey();

            set = new[] { 22, 7, 5, 2, 55, 11, 12, 67 };
            //QuickSort
            Console.WriteLine($"\n\n-----------------QuickSort------------------\n\n");
            Console.WriteLine($"Original Array: {String.Join(",", set)}");
            Console.WriteLine($"\nSorted Array: {String.Join(",", QuickSort.Sort(set, 0,set.Length-1))}");
            Console.WriteLine("Number of Iterations {0}", QuickSort.IterationsCounter);
            Console.ReadKey();
        }
Exemple #3
0
        static string[] CallQuickSort()
        {
            QuickSort quickSort = new QuickSort();

            string[] QS = new string[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                QS[i] = s[i];
            }

            Stopwatch stopwatch5 = new Stopwatch();

            stopwatch5.Start();

            quickSort.Sort(QS);

            stopwatch5.Stop();
            Console.WriteLine("\nQuick Sort - Time Elapsed: " + stopwatch5.Elapsed);

            //var temp = " ";
            //foreach (string str in QS)
            //{
            //    if (!str.StartsWith(temp[0]))
            //    {
            //        Console.Write("\n" + str[0] + ": ");
            //    }
            //    Console.Write(str + " ");
            //    temp = str;
            //}

            return(QS);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            //SelectionSort selectionSort = new SelectionSort();
            //selectionSort.Sort(selectionSort.InputArray);
            //Console.WriteLine($"Sorted Array: [{String.Join(", ", selectionSort.InputArray)}]");

            //BubbleSort bubbleSort = new BubbleSort();
            //bubbleSort.Sort(bubbleSort.InputArray);
            //Console.WriteLine($"Sorted Array: [{String.Join(", ", bubbleSort.InputArray)}]");

            //InsertionSort insertionSort = new InsertionSort();
            //insertionSort.Sort(insertionSort.InputArray);
            //insertionSort.Sort2(insertionSort.InputArray);
            //insertionSort.Sort3(insertionSort.InputArray);
            //Console.WriteLine($"Sorted Array: [{String.Join(", ", insertionSort.InputArray)}]");

            //MergeSort mergeSort = new MergeSort();
            //mergeSort.Sort(mergeSort.InputArray, 0, mergeSort.InputArray.Length - 1);
            //Console.WriteLine($"Sorted Array: [{String.Join(", ", mergeSort.InputArray)}]");

            QuickSort quickSort = new QuickSort();

            quickSort.Sort(quickSort.InputArray, 0, quickSort.InputArray.Length - 1);
            Console.WriteLine($"Sorted Array: [{String.Join(", ", quickSort.InputArray)}]");

            Console.ReadKey();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Select Sort Type! \n1. BubbleSort\n2. QuickSort\n3. MergeSort.\n");

            int sortType = int.Parse(Console.ReadLine());

            string[] a_temp = Console.ReadLine().Split(' ');
            int[]    array  = Array.ConvertAll(a_temp, Int32.Parse);

            //var array = new int[] {10,6,8,9,3,2};
            Util.PrintArray("Unsorted", array);

            switch (sortType)
            {
            case 1:
                Console.WriteLine("BubbleSort.");
                BubbleSort.Sort(array);
                break;

            case 2:
                Console.WriteLine("QuickSort.");
                QuickSort.Sort(array);
                break;

            case 3:
                Console.WriteLine("MergeSort.");
                MergeSort.Sort(array);
                break;
            }

            Util.PrintArray("  Sorted", array);
        }
Exemple #6
0
        private void btnQuickSort_Click(object sender, EventArgs e)
        {
            int[]     dizi = { 2, 17, -4, 42, 9, 26, 11, 3, 5, 28 };
            QuickSort QS   = new QuickSort();

            txtOnce.Text = QS.PrintItems(dizi);
            QS.Sort(dizi);
            txtSonra.Text = QS.PrintItems(dizi);
        }
        private static int[] RunQuickSort(int arraySize, int[] bigArray)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            var qs    = QuickSort.Sort((int[])bigArray.Clone(), 0, bigArray.Length - 1);

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

            return(qs);
        }
        static void Main(string[] args)
        {
            int[] theArray = ArrayTools.SequentialInts(10);
            Print(theArray);

            //ArrayTools.ShuffleArray<int> (theArray);
            ArrayTools.ReverseArray <int> (theArray);
            Print(theArray);

            QuickSort.Sort(theArray);
            Print(theArray);
        }
        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 #10
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 #11
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 #12
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 #13
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 #14
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;
                }
            }
        }