Exemplo n.º 1
0
        private void SelectionSortBtn_Click(object sender, EventArgs e)
        {
            var select = new SelectionSort <SortedItem>(items);

            BtnClick(select);
        }
Exemplo n.º 2
0
        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;
                    }
                }
            }
        }
Exemplo n.º 3
0
        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();
        }