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); }
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(); } }
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); }
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); } }
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); }