static void Main(string[] args) { var list = new int[] { 8, 56, 42, 4, 75, 1024, 2, 98, 3 }; foreach (var item in list) { Console.Write(item + " "); } Console.WriteLine(); var IS = new QuickSort <int>(); IS.Sort(list); foreach (var item in list) { Console.Write(item + " "); } Console.ReadLine(); }
private void QuickSortBrn_Click(object sender, EventArgs e) { var quick = new QuickSort <SortedItem>(items); BtnClick(quick); }
static void Main(string[] args) { int isContinue; bool check = false; do { try { do { Console.WriteLine("Input way: \n1)Manually \n2)Randomly"); var isManually = int.Parse(Console.ReadLine()); Console.Write("Input array size: "); var ArrayLength = int.Parse(Console.ReadLine()); int[] Array = new int[ArrayLength]; var sortBase = new SortBase(); Console.Write("Choose algorithm: \n1)Bubble sort \n2)Coctail sort \n3)Gnome sort \n4)Insertion sort \n5)Selection sort \n6)Shell sort \n7)Quick sort \n8)Marge sort \n"); var choose = int.Parse(Console.ReadLine()); switch (choose) { case 1: var bubbleSort = new BubbleSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Bubble sort"); var watchSort = System.Diagnostics.Stopwatch.StartNew(); bubbleSort.BubbleSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 2: var coctailSort = new CoctailSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Coctail sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); coctailSort.CoctailSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 3: var gnomeSort = new GnomeSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Gnome sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); gnomeSort.GnomeSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 4: var insertionSort = new InsertionSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Insertion sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); insertionSort.InsertionSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 5: var selectionSort = new SelectionSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Selection sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); selectionSort.SelectionSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 6: var shellSort = new ShellSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Shell sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); shellSort.ShellSorting(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 7: var quickSort = new QuickSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Quick sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); quickSort.QuickSorting(Array, ref ArrayLength); watchSort.Stop(); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; case 8: var mergeSort = new MergeSort(); if (isManually == 1) { sortBase.InputArray(Array, ref ArrayLength); } else { sortBase.RandomInputArray(Array, ref ArrayLength); sortBase.ShowArray(Array, ref ArrayLength); Console.WriteLine("\nPress <Enter> to sort"); Console.ReadLine(); } Console.WriteLine("Merge sort"); watchSort = System.Diagnostics.Stopwatch.StartNew(); mergeSort.MergeSorting(Array, ref ArrayLength); watchSort.Stop(); sortBase.ShowArray(Array, ref ArrayLength); Console.Write($"\nTime: {watchSort.Elapsed}"); break; default: Console.WriteLine("Invalid method choosing!"); break; } check = false; Console.WriteLine("\nRepeat? \n1)yes \n2)no"); isContinue = int.Parse(Console.ReadLine()); check = false; } while (isContinue == 1); } catch (FormatException) { Console.WriteLine("Invalid input!"); check = true; } } while (check); }
private void SortButton_Click(object sender, EventArgs e) { if (sortedItemsCount > 0) { (sender as Button).Enabled = false; int methods = 1; if (SpeedTrackBar.Value == 0) { SpeedTrackBar.Enabled = false; methods = allMethods.GetLength(0); VisualPanel.Controls.Clear(); } string methodName = ""; for (int methodNumber = 1; methodNumber <= methods; methodNumber++) { Label label = new Label(); AlgorithmBase <SortedItem> algorithm = new AlgorithmBase <SortedItem>(); if (SpeedTrackBar.Value > 0) { foreach (RadioButton radioButton in panel3.Controls.OfType <RadioButton>()) { if (radioButton.Checked) { if (!Int32.TryParse(radioButton.Name.Substring("radioButton".Length), out methodNumber)) { methodNumber = 1; } break; } } } if (panel3.Controls.Find("radioButton" + methodNumber.ToString(), false).Any()) { methodName = panel3.Controls["radioButton" + methodNumber.ToString()].Text; } switch (methodName) { case "Bubble Sort": algorithm = new BubbleSort <SortedItem>(); break; case "Cocktail Sort": algorithm = new CocktailSort <SortedItem>(); break; case "Insertion Sort": algorithm = new InsertionSort <SortedItem>(); break; case "Shell Sort": algorithm = new ShellSort <SortedItem>(); break; case "Tree Sort": algorithm = new Algorithm.DataStructures.Tree <SortedItem>(); break; case "Heap Sort": algorithm = new Algorithm.DataStructures.Heap <SortedItem>(); break; case "Selection Sort": algorithm = new SelectionSort <SortedItem>(); break; case "Gnome Sort": algorithm = new GnomeSort <SortedItem>(); break; case "Radix Sort": algorithm = new RadixSort <SortedItem>(RadixSortCheckBox.Checked); break; case "Merge Sort": algorithm = new MergeSort <SortedItem>(); break; case "Quick Sort": algorithm = new QuickSort <SortedItem>(); break; case "Odd-Even Sort": algorithm = new OddEvenSort <SortedItem>(); break; case "Comb Sort": algorithm = new CombSort <SortedItem>(); break; default: algorithm = new BubbleSort <SortedItem>(); break; } //MessageBox.Show("Вы выбрали метод сортировки " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " : " + methodNumber.ToString()); if (SpeedTrackBar.Value > 0) { algorithm.CompareEvent += AlgorithmCompareEvent; algorithm.SwapEvent += AlgorithmSwapEvent; algorithm.RemoveEvent += AlgorithmRemoveEvent; } else { int verticalInterval = 15; if (methodNumber > 1 && ((methodNumber - 1) % (VisualPanel.Height / verticalInterval)) == 0) { VisualPanel.Controls.Clear(); } label.Name = "label_" + methodNumber.ToString(); label.Text = "Идет сортировка массива из " + items.Count.ToString() + " элементов по методу " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " ..."; label.AutoSize = true; label.Location = new Point(5, verticalInterval * ((methodNumber - 1) % (VisualPanel.Height / verticalInterval))); VisualPanel.Controls.Add(label); VisualPanel.Refresh(); } algorithm.AddRange(items); TimeSpan runTime = algorithm.SortAndGetSpan(!reverseSortCheckBox.Checked); if (SpeedTrackBar.Value == 0) { label.Text = "Сортировка " + items.Count.ToString() + " элементов по методу " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " завершена."; } else if (methodName == "Heap Sort") { VisualPanel.Controls.Clear(); sortedItemsCount = 0; for (int i = 0; i < algorithm.Items.Count; i++) { SortedItem item = new SortedItem(VisualPanel, ++sortedItemsCount, algorithm.Items[i].Value); VisualPanel.Refresh(); } VisualPanel.Refresh(); } ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 3).Text = runTime.Seconds.ToString() + "." + runTime.Milliseconds.ToString(); ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 4).Text = algorithm.ComparisonCount.ToString(); ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 5).Text = algorithm.SwapCount.ToString(); VisualPanel.Refresh(); } SpeedTrackBar.Enabled = true; if (SpeedTrackBar.Value == 0) { for (int i = 1; i <= 3; i++) { ResultTableLayoutPanel.Controls["TestsRadioButton_" + i.ToString()].Enabled = true; } } } }
/// <summary> /// Prints result of sort algorithms used memory and time /// </summary> /// <param name="algorithms">Array of integers</param> /// <param name="minTime"></param> private static void PrintResult(int[] algorithms, double minTime) { for (int i = 0; i < algorithms.Length; i++) { switch (algorithms[i]) { case 1: if (InsertionSort.GetTime() == minTime) { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Insertion Sort Agorithm"); Console.WriteLine("Running time: {0} miliseconds", InsertionSort.GetTime()); Console.WriteLine("Memory usage: {0} bytes", InsertionSort.GetMemory()); Console.ForegroundColor = ConsoleColor.White; break; case 2: if (BubbleSort.GetTime() == minTime) { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Bubble Sort Agorithm"); Console.WriteLine("Running time: {0} miliseconds", BubbleSort.GetTime()); Console.WriteLine("Memory usage: {0} bytes", BubbleSort.GetMemory()); Console.ForegroundColor = ConsoleColor.White; break; case 3: if (QuickSort.GetTime() == minTime) { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Quick Sort Agorithm"); Console.WriteLine("Running time: {0} miliseconds", QuickSort.GetTime()); Console.WriteLine("Memory usage: {0} bytes", QuickSort.GetMemory()); Console.ForegroundColor = ConsoleColor.White; break; case 4: if (HeapSort.GetTime() == minTime) { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Heap Sort Agorithm"); Console.WriteLine("Running time: {0} miliseconds", HeapSort.GetTime()); Console.WriteLine("Memory usage: {0} bytes", HeapSort.GetMemory()); Console.ForegroundColor = ConsoleColor.White; break; case 5: if (MergeSort.GetTime() == minTime) { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Merge Sort Agorithm"); Console.WriteLine("Running time: {0} miliseconds", MergeSort.GetTime()); Console.WriteLine("Memory usage: {0} bytes", MergeSort.GetMemory()); Console.ForegroundColor = ConsoleColor.White; break; } } }
static void Main(string[] args) { BubbleSort <int> bubbleSort = new BubbleSort <int>(); CocktailSort <int> cocktailSort = new CocktailSort <int>(); InsertionSort <int> insertionSort = new InsertionSort <int>(); SelectionSort <int> selectionSort = new SelectionSort <int>(); ShellSort <int> shellSort = new ShellSort <int>(); StupidSort <int> stupidSort = new StupidSort <int>(); GnomeSort <int> gnomeSort = new GnomeSort <int>(); LSDRadixSort <int> lSDRadixSort = new LSDRadixSort <int>(); MSDRadixSort <int> mSDRadixSort = new MSDRadixSort <int>(); MergeSort <int> mergeSort = new MergeSort <int>(); QuickSort <int> quickSort = new QuickSort <int>(); var random = new Random(); for (int i = 0; i < 10; i++) { bubbleSort.Items.Add(random.Next(0, 100)); cocktailSort.Items.Add(random.Next(0, 100)); insertionSort.Items.Add(random.Next(0, 100)); selectionSort.Items.Add(random.Next(0, 100)); shellSort.Items.Add(random.Next(0, 100)); stupidSort.Items.Add(random.Next(0, 100)); gnomeSort.Items.Add(random.Next(0, 100)); lSDRadixSort.Items.Add(random.Next(0, 100)); mSDRadixSort.Items.Add(random.Next(0, 100)); mergeSort.Items.Add(random.Next(0, 100)); quickSort.Items.Add(random.Next(0, 100)); } Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < bubbleSort.Items.Count; i++) { Console.Write(" " + bubbleSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Bubble Sort: "); var bubbleSortTime = bubbleSort.Timer(); for (int i = 0; i < bubbleSort.Items.Count; i++) { Console.Write(" " + bubbleSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {bubbleSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {bubbleSort.SwapCount}."); Console.WriteLine($"Comparison Count: {bubbleSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < cocktailSort.Items.Count; i++) { Console.Write(" " + cocktailSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Cocktail Sort: "); var cocktailSortTime = cocktailSort.Timer(); for (int i = 0; i < cocktailSort.Items.Count; i++) { Console.Write(" " + cocktailSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {cocktailSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {cocktailSort.SwapCount}."); Console.WriteLine($"Comparison Count: {cocktailSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < insertionSort.Items.Count; i++) { Console.Write(" " + insertionSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Insertion Sort: "); var insertionSortTime = insertionSort.Timer(); for (int i = 0; i < insertionSort.Items.Count; i++) { Console.Write(" " + insertionSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {insertionSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {insertionSort.SwapCount}."); Console.WriteLine($"Comparison Count: {insertionSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < selectionSort.Items.Count; i++) { Console.Write(" " + selectionSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Selection Sort: "); var selectionSortTime = selectionSort.Timer(); for (int i = 0; i < selectionSort.Items.Count; i++) { Console.Write(" " + selectionSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {selectionSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {selectionSort.SwapCount}."); Console.WriteLine($"Comparison Count: {selectionSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < shellSort.Items.Count; i++) { Console.Write(" " + shellSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Shell Sort: "); var shellSortTime = shellSort.Timer(); for (int i = 0; i < shellSort.Items.Count; i++) { Console.Write(" " + shellSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {shellSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {shellSort.SwapCount}."); Console.WriteLine($"Comparison Count: {shellSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < stupidSort.Items.Count; i++) { Console.Write(" " + stupidSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Stupid Sort: "); var stupidSortTime = stupidSort.Timer(); for (int i = 0; i < stupidSort.Items.Count; i++) { Console.Write(" " + stupidSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {stupidSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {stupidSort.SwapCount}."); Console.WriteLine($"Comparison Count: {stupidSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < gnomeSort.Items.Count; i++) { Console.Write(" " + gnomeSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Gnome Sort: "); var gnomeSortTime = gnomeSort.Timer(); for (int i = 0; i < gnomeSort.Items.Count; i++) { Console.Write(" " + gnomeSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {gnomeSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {gnomeSort.SwapCount}."); Console.WriteLine($"Comparison Count: {gnomeSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < lSDRadixSort.Items.Count; i++) { Console.Write(" " + lSDRadixSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("LSD Radix Sort: "); var lSDRadixSortTime = lSDRadixSort.Timer(); for (int i = 0; i < lSDRadixSort.Items.Count; i++) { Console.Write(" " + lSDRadixSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {lSDRadixSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {lSDRadixSort.SwapCount}."); Console.WriteLine($"Comparison Count: {lSDRadixSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < mSDRadixSort.Items.Count; i++) { Console.Write(" " + lSDRadixSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("MSD Radix Sort: "); var mSDRadixSortTime = mSDRadixSort.Timer(); for (int i = 0; i < mSDRadixSort.Items.Count; i++) { Console.Write(" " + mSDRadixSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {mSDRadixSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {mSDRadixSort.SwapCount}."); Console.WriteLine($"Comparison Count: {mSDRadixSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < mergeSort.Items.Count; i++) { Console.Write(" " + mergeSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Merge Sort: "); var mergeSortTime = mergeSort.Timer(); for (int i = 0; i < mergeSort.Items.Count; i++) { Console.Write(" " + mergeSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {mergeSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {mergeSort.SwapCount}."); Console.WriteLine($"Comparison Count: {mergeSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.WriteLine("\n--------------------------"); Console.WriteLine("Array: "); for (int i = 0; i < quickSort.Items.Count; i++) { Console.Write(" " + quickSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine("Quick Sort: "); var quickSortTime = quickSort.Timer(); for (int i = 0; i < quickSort.Items.Count; i++) { Console.Write(" " + quickSort.Items[i]); } Console.WriteLine("\n"); Console.WriteLine($"Time: {quickSortTime.TotalMilliseconds}."); Console.WriteLine($"Swap Count: {quickSort.SwapCount}."); Console.WriteLine($"Comparison Count: {quickSort.ComparisonCount}."); Console.WriteLine("--------------------------\n"); Console.ReadKey(); }