Skip to content

Commit 8798b0b

Browse files
committed
Inital commit
0 parents  commit 8798b0b

File tree

13 files changed

+775
-0
lines changed

13 files changed

+775
-0
lines changed

InsertionSortAlgorithmVisualizer.exe

11 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29613.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsFormsApp1", "WindowsFormsApp1\WindowsFormsApp1.csproj", "{A10D50D4-0C24-4A02-8302-882CB7DBED8D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A10D50D4-0C24-4A02-8302-882CB7DBED8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A10D50D4-0C24-4A02-8302-882CB7DBED8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A10D50D4-0C24-4A02-8302-882CB7DBED8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A10D50D4-0C24-4A02-8302-882CB7DBED8D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5ABE4B55-DD56-49E9-8798-2F99BBB232B4}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>

insertion-sort-algorithm-visualizer/WindowsFormsApp1/Form1.Designer.cs

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using System.Data;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Windows.Forms;
6+
using System.Drawing.Drawing2D;
7+
8+
namespace WindowsFormsApp1
9+
{
10+
public partial class Form1 : Form
11+
{
12+
private int[] array;
13+
private int i = 0;
14+
private int j;
15+
private int key;
16+
private bool haveKey;
17+
18+
19+
public Form1()
20+
{
21+
InitializeComponent();
22+
}
23+
24+
protected void DrawArrow( Graphics graphics, Rectangle rectangle, int fromCol, int toCol, int fromRow, int toRow, bool arrowStart)
25+
{
26+
float width = rectangle.Width;
27+
float height = rectangle.Height;
28+
29+
Pen pen = new Pen(Color.DarkRed, 7);
30+
31+
32+
if (arrowStart)
33+
{
34+
pen.StartCap = LineCap.ArrowAnchor;
35+
}
36+
else
37+
{
38+
pen.EndCap = LineCap.ArrowAnchor;
39+
}
40+
41+
graphics.DrawLine(
42+
pen,
43+
width / 30 + (width / 15) * fromCol + fromCol * 10 ,
44+
height / 2 + ((width / 8) * fromRow) / 6,
45+
width / 30 + (width / 15) * toCol + toCol * 10,
46+
height / 2 + ((width / 8) * toRow) / 6
47+
);
48+
}
49+
50+
51+
protected void DrawArrayBoxes(int[] arr, Graphics graphics, Rectangle clientRectangle, int cell, int line, int[] blueRange, int[] pinkRange)
52+
{
53+
float width = clientRectangle.Width;
54+
float height = clientRectangle.Height;
55+
56+
57+
for (int index = 0; index < arr.Length; ++index)
58+
{
59+
RectangleF rectangleF = new RectangleF(
60+
width / 30 + (width / 15) * (index + cell) + 6 * (index + cell),
61+
height / 4 + 30 * line,
62+
width / 15,
63+
height / 7);
64+
65+
graphics.FillRectangle(Brushes.White, rectangleF);
66+
67+
if (blueRange.Contains(index))
68+
{
69+
graphics.FillRectangle(Brushes.LightBlue, rectangleF);
70+
}
71+
72+
73+
if (pinkRange.Contains(index))
74+
{
75+
graphics.FillRectangle(Brushes.Pink, rectangleF);
76+
}
77+
78+
79+
graphics.DrawString(
80+
arr[index].ToString(),
81+
new Font("Time New Rome", 16),
82+
Brushes.Black,
83+
new RectangleF(
84+
width / 30 + width / 15 * (index + cell) + 6 * (index + cell),
85+
height / 4 + 30 * line + 25,
86+
width / 15,
87+
height / 7 + 25),
88+
StringFormat.GenericDefault);
89+
90+
91+
graphics.DrawRectangle(
92+
new Pen(Brushes.Black),
93+
rectangleF.X,
94+
rectangleF.Y,
95+
rectangleF.Width,
96+
rectangleF.Height);
97+
}
98+
}
99+
100+
101+
private void buttonNext_Click(object sender, EventArgs e)
102+
{
103+
104+
if (array == null)
105+
{
106+
i = 0;
107+
j = 0;
108+
haveKey = false;
109+
array = textBoxNums.Text
110+
.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
111+
.Select(s => int.Parse(s))
112+
.ToArray();
113+
114+
textBoxNums.Visible = false;
115+
}
116+
117+
bool upArrow = false;
118+
bool downArrow = false;
119+
120+
Invalidate();
121+
Application.DoEvents();
122+
Graphics graphics = CreateGraphics();
123+
124+
if (i < array.Length)
125+
{
126+
127+
if (j >= 0 && haveKey && array[j] >= key)
128+
{
129+
array[j + 1] = array[j];
130+
DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[1] { j + 1 });
131+
DrawArrow( graphics, ClientRectangle, j, j + 1, 0, 0, false);
132+
133+
j--;
134+
}
135+
else if (haveKey)
136+
{
137+
array[j + 1] = key;
138+
DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[1] { j + 1 });
139+
140+
haveKey = false;
141+
downArrow = true;
142+
}
143+
else
144+
{
145+
i++;
146+
DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[0]);
147+
148+
if (i == array.Length)
149+
{
150+
return;
151+
}
152+
153+
key = array[i];
154+
haveKey = true;
155+
upArrow = true;
156+
j = i - 1;
157+
}
158+
159+
160+
DrawArrayBoxes(new int[1] { key }, graphics, ClientRectangle, j + 1, 1, new int[0], upArrow ? new int[1] : new int[0]);
161+
162+
if (!upArrow && !downArrow)
163+
{
164+
return;
165+
}
166+
167+
DrawArrow( graphics, ClientRectangle, j + 1, j + 1, upArrow ? 0 : 1, downArrow ? 0 : 1, true);
168+
169+
170+
}
171+
else
172+
{
173+
array = null;
174+
textBoxNums.Visible = true;
175+
}
176+
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)