Пример #1
0
        private void HeapSort_(Node[] input)
        {
            SelectLine(2);
            int heapSize = input.Length;

            NodeServices.Sleep(NodeServices.timeSleep);

            for (int p = (heapSize - 1) / 2; p >= 0 && SelectLine(3); p--)
            {
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(4);
                NodeServices.Sleep(NodeServices.timeSleep);
                MaxHeapify(input, heapSize, p);
                Clear_Label();
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(5);
            for (int i = input.Length - 1; i > 0; i--)
            {
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(7);
                NodeServices.Swap(ref input[0], ref input[i]);

                NodeServices.SetColor(input[i], Color.Green);
                NodeServices.DoEvent();

                SelectLine(8);
                heapSize--;
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(9);
                NodeServices.Sleep(NodeServices.timeSleep);
                MaxHeapify(input, heapSize, 0);
                Clear_Label();
            }
        }
Пример #2
0
        private void Quicksort_(Node[] input, int left, int right)
        {
            Clear_Label();
            if (left >= 0 && left < input.Length)
            {
                lLabel.Location = new Point(input[left].img.Location.X, NodeServices.Ynode + 50 + 30);
            }
            else
            {
                if (left < 0)
                {
                    lLabel.Location = new Point(input[0].img.Location.X - 100, NodeServices.Ynode + 50 + 30);
                }
                else
                {
                    lLabel.Location = new Point(input[input.Length - 1].img.Location.X + 100, NodeServices.Ynode + 50 + 30);
                }
            }
            if (right >= 0 && right < input.Length)
            {
                rLabel.Location = new Point(input[right].img.Location.X, NodeServices.Ynode - 50);
            }
            else
            {
                if (right < 0)
                {
                    rLabel.Location = new Point(input[0].img.Location.X - 100, NodeServices.Ynode + 50 + 30);
                }
                else
                {
                    rLabel.Location = new Point(input[input.Length - 1].img.Location.X + 100, NodeServices.Ynode + 50 + 30);
                }
            }

            SelectLine(2);
            NodeServices.Sleep(NodeServices.timeSleep);
            if (left >= right)
            {
                return;
            }

            Color color = Color.FromArgb(rad.Next(0, 256), rad.Next(0, 256), rad.Next(0, 256));

            for (int u = left; u <= right; u++)
            {
                input[u].img.BackColor = color;
            }
            NodeServices.DoEvent();

            SelectLine(3);
            int i = left, j = right;

            iLabel.Location = new Point(input[i].img.Location.X, NodeServices.Ynode - 30);
            iLabel.Text     = "i = " + i;
            jLabel.Location = new Point(input[j].img.Location.X, NodeServices.Ynode + 50 + 10);
            jLabel.Text     = "j = " + j;

            NodeServices.SetColor(input[i], Color.Orange);
            NodeServices.SetColor(input[j], Color.Pink);
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(4);
            int privot = input[(left + right) / 2].n;

            privotLabel.Text = "privot = " + input[(left + right) / 2].img.Text;
            NodeServices.DoEvent();
            NodeServices.Sleep(NodeServices.timeSleep);

            while (SelectLine(5) && i <= j)
            {
                NodeServices.Sleep(NodeServices.timeSleep);
                while (SelectLine(7) && input[i].n < privot)
                {
                    NodeServices.SetColor(input[i], color);
                    NodeServices.Sleep(NodeServices.timeSleep);

                    SelectLine(8);
                    i++;
                    iLabel.Location = new Point(input[i].img.Location.X, iLabel.Location.Y);
                    iLabel.Text     = "i = " + i;
                    NodeServices.SetColor(input[i], Color.Orange);
                    NodeServices.Sleep(NodeServices.timeSleep);
                }
                NodeServices.SetColor(input[i], Color.Blue);
                NodeServices.DoEvent();
                NodeServices.Sleep(NodeServices.timeSleep);

                while (SelectLine(9) && input[j].n > privot)
                {
                    NodeServices.SetColor(input[j], color);
                    NodeServices.Sleep(NodeServices.timeSleep);
                    SelectLine(10);
                    j--;
                    jLabel.Location = new Point(input[j].img.Location.X, jLabel.Location.Y);
                    jLabel.Text     = "j = " + j;
                    NodeServices.SetColor(input[j], Color.Pink);
                    NodeServices.Sleep(NodeServices.timeSleep);
                }

                NodeServices.SetColor(input[j], Color.Blue);
                NodeServices.DoEvent();
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(11);
                NodeServices.Sleep(NodeServices.timeSleep);
                if (i <= j)
                {
                    SelectLine(13);
                    NodeServices.Sleep(NodeServices.timeSleep);
                    if (i < j)
                    {
                        SelectLine(14);
                        NodeServices.Swap(ref input[i], ref input[j]);
                    }
                    NodeServices.SetColor(input[i], color);
                    NodeServices.SetColor(input[j], color);

                    SelectLine(15);
                    i++;
                    if (i < input.Length)
                    {
                        iLabel.Location = new Point(input[i].img.Location.X, iLabel.Location.Y);
                        iLabel.Text     = "i = " + i;
                        NodeServices.SetColor(input[i], Color.Orange);
                    }
                    NodeServices.Sleep(NodeServices.timeSleep);

                    SelectLine(16);
                    j--;

                    if (j >= 0)
                    {
                        jLabel.Location = new Point(input[j].img.Location.X, jLabel.Location.Y);
                        jLabel.Text     = "j = " + j;
                        NodeServices.SetColor(input[j], Color.Pink);
                    }
                    NodeServices.Sleep(NodeServices.timeSleep);
                }
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            for (int u = left; u <= right; u++)
            {
                input[u].img.BackColor = color;
            }
            SelectLine(19);
            NodeServices.Sleep(NodeServices.timeSleep);
            Quicksort_(input, left, j);

            SelectLine(20);
            NodeServices.Sleep(NodeServices.timeSleep);
            Quicksort_(input, i, right);

            SelectLine(21);
            NodeServices.Sleep(NodeServices.timeSleep);
        }
Пример #3
0
 public static void SetColor(Node t, Color color)
 {
     t.img.BackColor = color;
     NodeServices.DoEvent();
 }
Пример #4
0
        private void MaxHeapify(Node[] input, int heapSize, int index)
        {
            Clear_Label();
            SelectLine(12);
            lbHeapSize.Location = new Point(input[heapSize - 1].img.Location.X, input[heapSize - 1].img.Location.Y - 50);
            lbIndex.Location    = new Point(input[index].img.Location.X, input[index].img.Location.Y - 50);
            NodeServices.DoEvent();
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(14);
            int left = (index + 1) * 2 - 1;

            if (left < input.Length)
            {
                lbLeft.Location = new Point(input[left].img.Location.X, input[left].img.Location.Y + 80);
                NodeServices.DoEvent();
            }
            else
            {
                lbLeft.Location = new Point(input[input.Length - 1].img.Location.X + 100, input[input.Length - 1].img.Location.Y + 80);
                NodeServices.DoEvent();
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(15);
            int right = (index + 1) * 2;

            if (right < input.Length)
            {
                lbRight.Location = new Point(input[right].img.Location.X, input[right].img.Location.Y + 80);
                NodeServices.DoEvent();
            }
            else
            {
                lbRight.Location = new Point(lbLeft.Location.X + 100, lbLeft.Location.Y);
                NodeServices.DoEvent();
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(16);
            int pos = index;

            lbPos.Location = new Point(input[pos].img.Location.X, input[pos].img.Location.Y - 20);
            NodeServices.DoEvent();
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(17);
            NodeServices.Sleep(NodeServices.timeSleep);
            if (left < heapSize && input[left].n > input[pos].n)
            {
                SelectLine(18);
                pos            = left;
                lbPos.Location = new Point(input[pos].img.Location.X, input[pos].img.Location.Y - 20);
                NodeServices.DoEvent();
                NodeServices.Sleep(NodeServices.timeSleep);
            }

            SelectLine(19);
            NodeServices.Sleep(NodeServices.timeSleep);
            if (right < heapSize && input[right].n > input[pos].n)
            {
                SelectLine(20);
                pos            = right;
                lbPos.Location = new Point(input[pos].img.Location.X, input[pos].img.Location.Y - 20);
                NodeServices.DoEvent();
                NodeServices.Sleep(NodeServices.timeSleep);
            }

            SelectLine(21);
            NodeServices.Sleep(NodeServices.timeSleep);
            if (pos != index)
            {
                SelectLine(23);
                NodeServices.Swap(ref input[index], ref input[pos]);


                SelectLine(24);
                NodeServices.Sleep(NodeServices.timeSleep);
                MaxHeapify(input, heapSize, pos);
            }
        }
Пример #5
0
        private void btnReadFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Title = "Open file";
            openFileDialog.ShowDialog();
            string path = openFileDialog.FileName;

            if (path.Equals(""))
            {
                return;
            }

            StreamReader sr = null;

            try
            {
                sr = new StreamReader(path);
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] listString = line.Split(' ');
                    int[]    a          = new int[listString.Length];

                    for (int i = 0; i < listString.Length; i++)
                    {
                        a[i] = Int32.Parse(listString[i]);
                    }

                    if (listNode != null)
                    {
                        for (int i = 0; i < SoNode; i++)
                        {
                            this.Controls.Remove(listNode[i].img);
                            this.Controls.Remove(listNode[i].index);
                        }
                    }
                    listTextbox = null;

                    SoNode = listString.Length;
                    if (SoNode > 10)
                    {
                        SoNode = 10;
                    }
                    listNode = new Node[SoNode];

                    for (int i = 0; i < SoNode; i++)
                    {
                        Node node = NodeServices.CreateNode(a[i], new Point(i * 100 + 50, NodeServices.Ynode));
                        listNode[i] = node;
                        this.Controls.Add(node.img);
                        this.Controls.Add(node.index);
                    }

                    NodeServices.DoEvent();
                    break;
                }
            }
            catch (Exception)
            {
                string st      = "Khong the doc du lieu tu file da cho";
                string caption = "Error";
                MessageBox.Show(st, caption, MessageBoxButtons.OK);
            }
            finally
            {
                sr.Close();
            }

            btnManual.Enabled = false;
            btnRandom.Enabled = false;
            btnSort.Enabled   = true;
        }