public static void Remove(SortedItem item)
        {
            Control control = null;

            if (item.VerticalProgressBar != null)
            {
                control = item.VerticalProgressBar.Parent;
                control.Controls.Remove(item.VerticalProgressBar);
            }
            if (item.Label != null)
            {
                if (control == null)
                {
                    control = item.Label.Parent;
                }
                control.Controls.Remove(item.Label);
            }
            if (item.LabelID != null)
            {
                if (control == null)
                {
                    control = item.LabelID.Parent;
                }
                control.Controls.Remove(item.LabelID);
            }
            item = null;
            Replace(control);
        }
Beispiel #2
0
 private void AddButton_Click(object sender, EventArgs e)
 {
     if (int.TryParse(AddTextBox.Text, out int value))
     {
         var item = new SortedItem(value, items.Count);
         items.Add(item);
     }
     RefreshItems();
     AddTextBox.Text = "";
 }
Beispiel #3
0
        private void AlgorithmRemoveEvent(object sender, SortedItem e)
        {
            if (SpeedTrackBar.Value > 0)
            {
                e.SetColor(Color.Black);

                VisualPanel.Refresh();

                SortedItem.Remove(e);

                VisualPanel.Refresh();
            }
        }
Beispiel #4
0
 private void FillButton_Click(object sender, EventArgs e)
 {
     if (int.TryParse(FillTextBox.Text, out int value))
     {
         var rnd = new Random();
         for (int i = 0; i < value; i++)
         {
             var item = new SortedItem(rnd.Next(0, 100), items.Count);
             items.Add(item);
         }
     }
     RefreshItems();
     FillTextBox.Text = "";
 }
        public static void SwapPosition(SortedItem first, SortedItem second)
        {
            if (first.VerticalProgressBar != null && second.VerticalProgressBar != null && first != second)
            {
                // Swap location
                Point locationFirst = first.VerticalProgressBar.Location;
                first.VerticalProgressBar.Location  = second.VerticalProgressBar.Location;
                second.VerticalProgressBar.Location = locationFirst;

                locationFirst         = first.Label.Location;
                first.Label.Location  = second.Label.Location;
                second.Label.Location = locationFirst;

                locationFirst           = first.LabelID.Location;
                first.LabelID.Location  = second.LabelID.Location;
                second.LabelID.Location = locationFirst;

                // Swap index
                //int index = first.Index;
                //first.Index = second.Index;
                //second.Index = first.Index;

                int index = first.VerticalProgressBar.TabIndex;
                first.VerticalProgressBar.TabIndex  = second.VerticalProgressBar.TabIndex;
                second.VerticalProgressBar.TabIndex = index;

                first.Label.TabIndex  = second.Label.TabIndex;
                second.Label.TabIndex = index;

                first.LabelID.Text  = second.LabelID.Text;
                first.LabelID.Text  = second.LabelID.Text;
                second.LabelID.Text = "[" + index.ToString() + "]";

                // Swap name
                string name = first.VerticalProgressBar.Name;
                first.VerticalProgressBar.Name  = second.VerticalProgressBar.Name;
                second.VerticalProgressBar.Name = name;

                name              = first.Label.Name;
                first.Label.Name  = second.Label.Name;
                second.Label.Name = name;

                name = first.LabelID.Name;
                first.LabelID.Name  = second.LabelID.Name;
                second.LabelID.Name = name;
            }
        }
        private void AddButton_Click(object sender, EventArgs e)
        {
            if (int.TryParse(AddTextBox.Text, out int value))
            {
                if (value > 99)
                {
                    AddTextBox.Text = "99";
                    return;
                }
                var item = new SortedItem(value, items.Count);
                items.Add(item);

                DrawItems(items);
            }

            AddTextBox.Text = "";
        }
Beispiel #7
0
        private void AddNumberButton_Click(object sender, EventArgs e)
        {
            if (int.TryParse(AddTextBox.Text, out int value) && value <= maxValue)
            {
                if (sortedItemsCount >= maxSortedItemsCount)
                {
                    VisualPanel.Controls.Clear();
                    sortedItemsCount = 0;
                    items.Clear();
                    values.Clear();
                }

                SortedItem item = new SortedItem(VisualPanel, ++sortedItemsCount, value);
                items.Add(item);
                values.Add(value);
            }
            AddTextBox.Text = "";
            AddTextBox.Focus();
        }
Beispiel #8
0
        private void AlgorithmSwapEvent(object sender, Tuple <SortedItem, SortedItem> e)
        {
            if (SpeedTrackBar.Value > 0)
            {
                e.Item1.SetColor(Color.Red);
                e.Item2.SetColor(Color.Red);

                VisualPanel.Refresh();

                SortedItem.SwapPosition(e.Item1, e.Item2);

                System.Threading.Thread.Sleep(delayMilliSeconds * SpeedTrackBar.Value - 1);

                e.Item1.SetColor(Color.Blue);
                e.Item2.SetColor(Color.Blue);

                VisualPanel.Refresh();
            }
        }
Beispiel #9
0
 private void RefillItems(bool enableVisualization = true)
 {
     if (enableVisualization)
     {
         VisualPanel.Controls.Clear();
     }
     sortedItemsCount = 0;
     items.Clear();
     for (int i = 0; i < values.Count; i++)
     {
         if (enableVisualization)
         {
             SortedItem item = new SortedItem(VisualPanel, ++sortedItemsCount, values[i]);
             items.Add(item);
             VisualPanel.Refresh();
         }
         else
         {
             SortedItem item = new SortedItem(++sortedItemsCount, values[i]);
             items.Add(item);
         }
     }
 }
Beispiel #10
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;
                    }
                }
            }
        }
Beispiel #11
0
        private void button2_Click(object sender, EventArgs e)
        {
            var sw = new Stopwatch();

            Thread.Sleep(1500);
            sw.Start();
            var rnd = new Random();

            for (int i = 0; i < 20; i++)
            {
                var val  = rnd.Next(0, 100);
                var item = new SortedItem(val);
                items.Add(item);
                SetProperty(label3, item);
            }
            //---------------
            sw.Restart();
            var bubble = new BubbleSort <SortedItem>();

            bubble.Items.AddRange(items);
            bubble.Sort();
            foreach (var i in bubble.Items)
            {
                SetProperty(label4, i);
            }
            sw.Stop();
            label4.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //----------------
            sw.Restart();
            var coctail = new KoktailSort <SortedItem>();

            coctail.Items.AddRange(items);
            coctail.Sort();
            foreach (var i in coctail.Items)
            {
                SetProperty(label5, i);
            }
            sw.Stop();
            label5.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //-----------------
            sw.Restart();
            var insert = new InsertSort <SortedItem>();

            insert.Items.AddRange(items);
            insert.Sort();
            foreach (var i in insert.Items)
            {
                SetProperty(label6, i);
            }
            sw.Stop();
            label6.Text += $"\t  {sw.ElapsedMilliseconds.ToString()} мс";
            //----------------------
            sw.Restart();
            var shell = new ShellSort <SortedItem>();

            shell.Items.AddRange(items);
            shell.Sort();
            foreach (var i in shell.Items)
            {
                SetProperty(label7, i);
            }
            sw.Stop();
            label7.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //----------------------
            items.Clear();
        }
Beispiel #12
0
 private void SetProperty(Label lb, SortedItem value)
 {
     lb.Text += "  " + value;
     lb.Font  = new Font("Tobota", 10, FontStyle.Bold);
 }