Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //SelectionSort selectionSort = new SelectionSort();
            //selectionSort.PerformSort();

            //BubbleSort bubbleSort = new BubbleSort();
            //bubbleSort.PerformSort();

            //InsertionSort insertionSort = new InsertionSort();
            //insertionSort.PerformSort();

            //MergeSort mergeSort = new MergeSort();
            //mergeSort.PerformSort();

            //HeapSort heapSort = new HeapSort();
            //heapSort.PerformSort();

            //QuickSort quickSort = new QuickSort();
            //quickSort.PerformSort();

            RadixSort radixSort = new RadixSort();

            radixSort.PerformSort();
        }
Ejemplo 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;
                    }
                }
            }
        }