Пример #1
0
        private void Move_A(Node t, int X, int Y)
        {
            int u = 0;

            while (u <= 60)
            {
                t.img.Location = new Point(t.img.Location.X, t.img.Location.Y + 1);
                NodeServices.Sleep(NodeServices.delay);
                u++;
            }
            while (t.img.Location.X != X)
            {
                if (t.img.Location.X > X)
                {
                    t.img.Location = new Point(t.img.Location.X - 1, t.img.Location.Y);
                }
                else
                {
                    t.img.Location = new Point(t.img.Location.X + 1, t.img.Location.Y);
                }
                NodeServices.Sleep(NodeServices.delay);
            }
            while (t.img.Location.Y < NodeServices.Ynode)
            {
                t.img.Location = new Point(t.img.Location.X, t.img.Location.Y + 1);
                NodeServices.Sleep(NodeServices.delay);
            }
        }
Пример #2
0
        private void btnCreateListNode_Click(object sender, EventArgs e)
        {
            if (listNode != null)
            {
                for (int i = 0; i < SoNode; i++)
                {
                    this.Controls.Remove(listNode[i].img);
                    this.Controls.Remove(listNode[i].index);
                }
            }
            if (listTextbox != null)
            {
                listTextbox = null;
            }

            btnRandom.Enabled = true;
            btnManual.Enabled = true;
            SoNode            = Int32.Parse(this.numNodeNumber.Value.ToString());
            listNode          = new Node[SoNode];
            for (int i = 0; i < SoNode; i++)
            {
                Node a = NodeServices.CreateNode(0, new Point(i * 100 + 50, NodeServices.Ynode));
                a.img.Text   = "";
                a.index.Text = "[" + i + "]";
                this.Controls.Add(a.img);
                this.Controls.Add(a.index);
                listNode[i] = a;
            }
            Application.DoEvents();

            btnSort.Enabled     = false;
            btnPause.Enabled    = false;
            btnContinue.Enabled = false;
            btnStop.Enabled     = false;
        }
Пример #3
0
        public static void Swap(ref Node t1, ref Node t2)
        {
            bool isSwap = false;

            Node t;

            if (t1.img.Location.X > t2.img.Location.X)
            {
                t      = t1;
                t1     = t2;
                t2     = t;
                isSwap = true;
            }

            Point NewLocation1 = new Point(t2.img.Location.X, t2.img.Location.Y);
            Point NewLocation2 = new Point(t1.img.Location.X, t1.img.Location.Y);

            while (t1.img.Location.Y > 120)
            {
                t1.img.Location = new Point(t1.img.Location.X, t1.img.Location.Y - 1);
                t2.img.Location = new Point(t2.img.Location.X, t2.img.Location.Y + 1);

                NodeServices.Sleep(delay);
            }

            while (t1.img.Location.X != NewLocation1.X)
            {
                t1.img.Location = new Point(t1.img.Location.X + 1, t1.img.Location.Y);
                t2.img.Location = new Point(t2.img.Location.X - 1, t2.img.Location.Y);

                NodeServices.Sleep(delay);
            }


            while (t1.img.Location.Y != NewLocation1.Y)
            {
                t1.img.Location = new Point(t1.img.Location.X, t1.img.Location.Y + 1);
                t2.img.Location = new Point(t2.img.Location.X, t2.img.Location.Y - 1);

                NodeServices.Sleep(delay);
            }

            if (!isSwap)
            {
                t  = t1;
                t1 = t2;
                t2 = t;
            }
            while (Form1.debug && Form1.isDebug)
            {
                DoEvent();
            }
            Form1.debug = true;
        }
Пример #4
0
        public void Start(Node[] listNode)
        {
            iLabel  = CreateLabel("i", Color.Orange);
            jLabel  = CreateLabel("j", Color.Pink);
            j_Label = CreateLabel("j-1", Color.Blue);
            parent.Controls.Add(iLabel);
            parent.Controls.Add(jLabel);
            parent.Controls.Add(j_Label);
            Clear_Label();

            for (int i = 0; SelectLine(2) && i < listNode.Length; i++)
            {
                NodeServices.SetColor(listNode[i], Color.Orange);
                iLabel.Location = new Point(listNode[i].img.Location.X, listNode[i].img.Location.Y - 30);
                iLabel.Text     = "i = " + i;
                NodeServices.Sleep(NodeServices.timeSleep);

                for (int j = listNode.Length - 1; SelectLine(3) && j > i; j--)
                {
                    NodeServices.SetColor(listNode[j], Color.Pink);
                    jLabel.Location = new Point(listNode[j].img.Location.X, listNode[j].img.Location.Y - 30);
                    jLabel.Text     = "j = " + j;
                    NodeServices.SetColor(listNode[j - 1], Color.Blue);
                    j_Label.Location = new Point(listNode[j - 1].img.Location.X, listNode[j - 1].img.Location.Y + 50 + 10);
                    j_Label.Text     = "j-1 = " + (j - 1);
                    NodeServices.Sleep(NodeServices.timeSleep);

                    listCode.SelectedIndex = 4;
                    NodeServices.Sleep(NodeServices.timeSleep);
                    if (listNode[j].n < listNode[j - 1].n)
                    {
                        listCode.SelectedIndex = 5;
                        NodeServices.Swap(ref listNode[j], ref listNode[j - 1]);
                        NodeServices.SetColor(listNode[j], NodeServices.DefaultColor);
                    }
                    else
                    {
                        NodeServices.SetColor(listNode[j], NodeServices.DefaultColor);
                    }
                }
                NodeServices.Sleep(NodeServices.timeSleep);

                NodeServices.SetColor(listNode[i], Color.Green);
                if (i != listNode.Length - 1)
                {
                    NodeServices.SetColor(listNode[i + 1], NodeServices.DefaultColor);
                }
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(6);
            NodeServices.Sleep(NodeServices.timeSleep);
        }
Пример #5
0
        private void MergeSort_(Node[] input, int low, int high)
        {
            Clear_Label();
            lowLabel.Location  = new Point(input[low].img.Location.X, input[low].img.Location.Y - 50);
            highLabel.Location = new Point(input[high].img.Location.X, input[high].img.Location.Y + 30 + 50);
            SelectLine(2);
            NodeServices.Sleep(NodeServices.timeSleep);
            if (low < high)
            {
                SelectLine(4);
                int middle = (low / 2) + (high / 2);
                midLabel.Location = new Point(input[middle].img.Location.X, input[middle].img.Location.Y + 30 + 50);
                NodeServices.Sleep(NodeServices.timeSleep);

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

                SelectLine(5);
                NodeServices.Sleep(NodeServices.timeSleep);

                Pen penclear = new Pen(Color.White, 2);

                Pen pen1 = new Pen(color1, 2);
                g.DrawLine(pen1, input[low].img.Location.X - (middle - low + 1) * 2, NodeServices.Ynode - (middle - low + 1) * 2, input[low].img.Location.X - (middle - low + 1) * 2, NodeServices.Ynode + 50 + (middle - low + 1) * 2);
                g.DrawLine(pen1, input[middle].img.Location.X + (middle - low + 1) * 2 + 50, NodeServices.Ynode - (middle - low + 1) * 2, input[middle].img.Location.X + (middle - low + 1) * 2 + 50, NodeServices.Ynode + 50 + (middle - low + 1) * 2);

                Pen pen2 = new Pen(color2, 2);
                g.DrawLine(pen2, input[middle + 1].img.Location.X - (high - (middle + 1) + 1) * 2, NodeServices.Ynode - (high - (middle + 1) + 1) * 2, input[middle + 1].img.Location.X - (high - (middle + 1) + 1) * 2, NodeServices.Ynode + 50 + (high - (middle + 1) + 1) * 2);
                g.DrawLine(pen2, input[high].img.Location.X + (high - (middle + 1) + 1) * 2 + 50, NodeServices.Ynode - (high - (middle + 1) + 1) * 2, input[high].img.Location.X + (high - (middle + 1) + 1) * 2 + 50, NodeServices.Ynode + 50 + (high - (middle + 1) + 1) * 2);

                MergeSort_(input, low, middle);

                g.DrawLine(penclear, input[low].img.Location.X - (middle - low + 1) * 2, NodeServices.Ynode - (middle - low + 1) * 2, input[low].img.Location.X - (middle - low + 1) * 2, NodeServices.Ynode + 50 + (middle - low + 1) * 2);
                g.DrawLine(penclear, input[middle].img.Location.X + (middle - low + 1) * 2 + 50, NodeServices.Ynode - (middle - low + 1) * 2, input[middle].img.Location.X + (middle - low + 1) * 2 + 50, NodeServices.Ynode + 50 + (middle - low + 1) * 2);

                SelectLine(6);
                NodeServices.Sleep(NodeServices.timeSleep);
                MergeSort_(input, middle + 1, high);
                g.DrawLine(penclear, input[middle + 1].img.Location.X - (high - (middle + 1) + 1) * 2, NodeServices.Ynode - (high - (middle + 1) + 1) * 2, input[middle + 1].img.Location.X - (high - (middle + 1) + 1) * 2, NodeServices.Ynode + 50 + (high - (middle + 1) + 1) * 2);
                g.DrawLine(penclear, input[high].img.Location.X + (high - (middle + 1) + 1) * 2 + 50, NodeServices.Ynode - (high - (middle + 1) + 1) * 2, input[high].img.Location.X + (high - (middle + 1) + 1) * 2 + 50, NodeServices.Ynode + 50 + (high - (middle + 1) + 1) * 2);

                SelectLine(7);
                lowLabel.Location  = new Point(input[low].img.Location.X, input[low].img.Location.Y - 50);
                highLabel.Location = new Point(input[high].img.Location.X, input[high].img.Location.Y + 30 + 50);
                midLabel.Location  = new Point(input[middle].img.Location.X, input[middle].img.Location.Y + 30 + 50);
                NodeServices.Sleep(NodeServices.timeSleep);
                Merge(input, low, middle, high);

                SelectLine(9);
                NodeServices.Sleep(NodeServices.timeSleep);
            }
        }
Пример #6
0
        private void btnRandom_Click(object sender, EventArgs e)
        {
            listTextbox = null;
            for (int i = 0; i < SoNode; i++)
            {
                this.Controls.Remove(listNode[i].img);
                this.Controls.Remove(listNode[i].index);
                Node a = NodeServices.CreateNode(rad.Next(0, 100), new Point(i * 100 + 50, NodeServices.Ynode));
                a.index.Text = "[" + i + "]";
                this.Controls.Add(a.img);
                this.Controls.Add(a.index);
                listNode[i] = a;
            }

            Application.DoEvents();
            btnSort.Enabled = true;
        }
Пример #7
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();
            }
        }
Пример #8
0
        private void Merge(Node[] input, int low, int middle, int high)
        {
            int left  = low;
            int right = middle + 1;

            Node[] tmp      = new Node[(high - low) + 1];
            int    tmpIndex = 0;

            for (int i = left; i <= middle; i++)
            {
                int u = 0;
                while (u < 120)
                {
                    input[i].img.Location = new Point(input[i].img.Location.X, input[i].img.Location.Y - 1);
                    NodeServices.Sleep(NodeServices.delay);
                    u++;
                }
            }

            for (int i = right; i <= high; i++)
            {
                int u = 0;
                while (u < 120)
                {
                    input[i].img.Location = new Point(input[i].img.Location.X, input[i].img.Location.Y + 1);
                    NodeServices.Sleep(NodeServices.delay);
                    u++;
                }
            }

            while ((left <= middle) && (right <= high))
            {
                if (input[left].n < input[right].n)
                {
                    Move_A(input[left], (tmpIndex + low) * 100 + 50, NodeServices.Ynode);
                    tmp[tmpIndex] = input[left];
                    left          = left + 1;
                }
                else
                {
                    Move_B(input[right], (tmpIndex + low) * 100 + 50, NodeServices.Ynode);
                    tmp[tmpIndex] = input[right];
                    right         = right + 1;
                }
                tmpIndex = tmpIndex + 1;
            }

            if (left <= middle)
            {
                while (left <= middle)
                {
                    Move_A(input[left], (tmpIndex + low) * 100 + 50, NodeServices.Ynode);
                    tmp[tmpIndex] = input[left];
                    left          = left + 1;
                    tmpIndex      = tmpIndex + 1;
                }
            }

            if (right <= high)
            {
                while (right <= high)
                {
                    Move_B(input[right], (tmpIndex + low) * 100 + 50, NodeServices.Ynode);
                    tmp[tmpIndex] = input[right];
                    right         = right + 1;
                    tmpIndex      = tmpIndex + 1;
                }
            }

            for (int i = 0; i < tmp.Length; i++)
            {
                input[low + i] = tmp[i];
            }
        }
Пример #9
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);
        }
Пример #10
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);
            }
        }
Пример #11
0
 public static void SetColor(Node t, Color color)
 {
     t.img.BackColor = color;
     NodeServices.DoEvent();
 }
Пример #12
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;
        }
Пример #13
0
        public void Start(Node[] listNode)
        {
            iLabel = CreateLabel("i", Color.Orange);
            jLabel = CreateLabel("j", Color.Pink);
            xLabel = CreateLabel("x", Color.Blue);
            parent.Controls.Add(iLabel);
            parent.Controls.Add(jLabel);
            parent.Controls.Add(xLabel);
            Clear_Label();

            for (int i = 0; SelectLine(2) && i < listNode.Length; i++)
            {
                NodeServices.SetColor(listNode[i], Color.Orange);
                iLabel.Location = new Point(listNode[i].img.Location.X, listNode[i].img.Location.Y - 30);
                iLabel.Text     = "i = " + i;
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(4);
                int x = i;
                xLabel.Location = new Point(listNode[x].img.Location.X, listNode[x].img.Location.Y + 50 + 10);
                xLabel.Text     = "x = " + x;
                NodeServices.Sleep(NodeServices.timeSleep);

                for (int j = i + 1; SelectLine(5) && j < listNode.Length; j++)
                {
                    NodeServices.SetColor(listNode[j], Color.Pink);
                    jLabel.Location = new Point(listNode[j].img.Location.X, listNode[j].img.Location.Y - 30);
                    jLabel.Text     = "j = " + j;
                    NodeServices.Sleep(NodeServices.timeSleep);

                    SelectLine(6);
                    NodeServices.Sleep(NodeServices.timeSleep);
                    if (listNode[x].n > listNode[j].n)
                    {
                        if (x != i)
                        {
                            NodeServices.SetColor(listNode[x], NodeServices.DefaultColor);
                        }

                        SelectLine(7);
                        x = j;
                        NodeServices.SetColor(listNode[x], Color.Blue);
                        xLabel.Location = new Point(listNode[x].img.Location.X, listNode[x].img.Location.Y + 50 + 10);
                        xLabel.Text     = "x = " + x;
                        NodeServices.Sleep(NodeServices.timeSleep);
                    }
                    else
                    {
                        NodeServices.SetColor(listNode[j], NodeServices.DefaultColor);
                    }
                }
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(8);
                NodeServices.Sleep(NodeServices.timeSleep);
                if (x != i)
                {
                    SelectLine(9);
                    NodeServices.Swap(ref listNode[i], ref listNode[x]);
                    NodeServices.SetColor(listNode[x], NodeServices.DefaultColor);
                }

                NodeServices.SetColor(listNode[i], Color.Green);
            }
            NodeServices.Sleep(NodeServices.timeSleep);

            SelectLine(11);
            NodeServices.Sleep(NodeServices.timeSleep);
        }
Пример #14
0
        public void Start(Node[] listNode)
        {
            xLabel          = CreateLabel("x: ", Color.Transparent);
            xLabel.Location = new Point(0, 120);
            parent.Controls.Add(xLabel);

            posLabel          = CreateLabel("pos", Color.Gray);
            posLabel.Location = new Point(1500, 1500);
            posLabel.Size     = new Size(50, 20);
            posLabel.Font     = new Font("Arial", 10, FontStyle.Bold);
            parent.Controls.Add(posLabel);

            int  x;
            Node temp;

            NodeServices.SetColor(listNode[0], Color.Green);
            SelectLine(2);
            NodeServices.Sleep(NodeServices.timeSleep);

            for (int i = 1; SelectLine(3) && i < listNode.Length; i++)
            {
                NodeServices.SetColor(listNode[i], Color.Orange);
                NodeServices.Sleep(NodeServices.timeSleep);

                Point locationEmpty = new Point(listNode[i].img.Location.X, listNode[i].img.Location.Y);
                while (SelectLine(5) && listNode[i].img.Location.Y > 120)
                {
                    listNode[i].img.Location = new Point(listNode[i].img.Location.X, listNode[i].img.Location.Y - 1);
                    NodeServices.Sleep(NodeServices.delay);
                }
                NodeServices.Sleep(11);
                temp = listNode[i];

                x = i - 1;
                posLabel.Location      = new Point(listNode[x].img.Location.X, listNode[x].img.Location.Y - 30);
                listCode.SelectedIndex = 6;
                NodeServices.Sleep(NodeServices.timeSleep);

                while (SelectLine(7) && x >= 0 && listNode[x].n > temp.n)
                {
                    NodeServices.Sleep(NodeServices.timeSleep);

                    Point xLocation = listNode[x].img.Location;
                    SelectLine(9);
                    while (listNode[x].img.Location.X < locationEmpty.X)
                    {
                        listNode[x].img.Location = new Point(listNode[x].img.Location.X + 1, listNode[x].img.Location.Y);
                        NodeServices.Sleep(NodeServices.delay);
                    }
                    NodeServices.Sleep(11);

                    locationEmpty   = xLocation;
                    listNode[x + 1] = listNode[x];

                    SelectLine(10);
                    x--;
                    if (x >= 0)
                    {
                        posLabel.Location = new Point(listNode[x].img.Location.X, listNode[x].img.Location.Y - 30);
                    }
                    else
                    {
                        posLabel.Location = new Point(0, NodeServices.Ynode - 30);
                    }
                    NodeServices.Sleep(NodeServices.timeSleep);
                }
                NodeServices.Sleep(NodeServices.timeSleep);

                SelectLine(12);
                while (temp.img.Location.X != locationEmpty.X)
                {
                    if (temp.img.Location.X > locationEmpty.X)
                    {
                        temp.img.Location = new Point(temp.img.Location.X - 1, temp.img.Location.Y);
                        NodeServices.Sleep(NodeServices.delay);
                    }
                    else
                    {
                        temp.img.Location = new Point(temp.img.Location.X + 1, temp.img.Location.Y);
                        NodeServices.Sleep(NodeServices.delay);
                    }
                }

                while (temp.img.Location.Y != locationEmpty.Y)
                {
                    temp.img.Location = new Point(temp.img.Location.X, temp.img.Location.Y + 1);
                    NodeServices.Sleep(NodeServices.delay);
                }
                NodeServices.Sleep(11);

                listNode[x + 1] = temp;
                NodeServices.SetColor(listNode[x + 1], Color.Green);
            }
            SelectLine(14);
            NodeServices.Sleep(NodeServices.timeSleep);
        }