📄 form1.cs
字号:
this.btnGenerate,
this.plInsertion,
this.plBubble,
this.plSelection,
this.label10,
this.lbDelay,
this.btClearDisplay});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "Sorting Algorithm Analysis";
this.plBubble.ResumeLayout(false);
this.plInsertion.ResumeLayout(false);
this.plSelection.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnGenerate_Click(object sender, System.EventArgs e)
{
clearAll();
mode = "normal";
delay = Convert.ToInt32(tbDelay.Text);
total = Convert.ToInt32(tbTotal.Text);
list = new int[total];
list1 = new int[total];
list2= new int[total];
list3= new int[total];
tbList.Clear();
for(int i=0; i<total; i++)
{
list[i] = rnd.Next(100);
list1[i] = list[i];
list2[i] = list[i];
list3[i] = list[i];
//tbList.AppendText((i+1)+ ". " + list[i] + "\n");
tbList.AppendText(list[i]+ " ");
}
//list1 = list;
//list2 = list;
//list3 = list;
/*
textBox1.AppendText("\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list1[i]+ " ");
}
textBox1.AppendText("\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list2[i]+ " ");
}
textBox1.AppendText("\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list3[i]+ " ");
}
*/
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if(!mode.Equals("race"))
{
clearAll();
mode = "race";
Thread thBubble = new Thread(new ThreadStart(bubbleSort));
thBubble.Start();
Thread thInsertion = new Thread(new ThreadStart(insertionSort));
thInsertion.Start();
Thread thSelection = new Thread(new ThreadStart(selectionSort));
thSelection.Start();
}
else
MessageBox.Show("Click GENERATE to start with a new list.");
}
public void bubbleSort()
{
int row = 0, column = 0;
start = new TimeSpan(System.DateTime.Now.Ticks);
lbStart_Bubble.Text = start.ToString();
this.lbStart_Bubble.Refresh();
for(row = 1; row <= list1.Length; row++)
{
for(column =0; column < list1.Length-1; column++)
{
Thread.Sleep(delay);
tbResult_Bubble.Clear();
for(int i=0; i<list1.Length; i++)
tbResult_Bubble.AppendText(list1[i] + " " );
if(list1[column]>list1[column+1])
swap(list1[column], list1[column+1], column, column+1);
}
display(list1);
}
end = new TimeSpan(System.DateTime.Now.Ticks);
lbEnd_Bubble.Text = end.ToString();
lbTotal_Bubble.Text = end.Subtract(start).ToString();
tbResult_Bubble.Clear();
for(int i=0; i<list1.Length; i++)
tbResult_Bubble.AppendText(list1[i] + " " );
}
public void selectionSort()
{
int row = 0, column = 0, min=0, temp = 0;
start = new TimeSpan(System.DateTime.Now.Ticks);
lbStart_Selection.Text = start.ToString();
this.lbStart_Selection.Refresh();
for(row = 0; row < list2.Length; row++)
{
min = row;
for(column = row; column < list2.Length; column++)
{
Thread.Sleep(delay);
tbResult_Selection.Clear();
for(int i=0; i<list2.Length; i++)
tbResult_Selection.AppendText(list2[i] + " " );
if(list2[min] > list2[column])
min = column;
}
temp = list2[min];
list2[min] = list2[row];
list2[row] = temp;
display(list2);
}
end = new TimeSpan(System.DateTime.Now.Ticks);
lbEnd_Selection.Text = end.ToString();
lbTotal_Selection.Text = end.Subtract(start).ToString();
tbResult_Selection.Clear();
for(int i=0; i<list2.Length; i++)
tbResult_Selection.AppendText(list2[i] + " " );
}
public void insertionSort()
{
int row = 0, column = 0, max = 0;
start = new TimeSpan(System.DateTime.Now.Ticks);
lbStart_Insertion.Text = start.ToString();
this.lbStart_Insertion.Refresh();
for(row=1; row<list3.Length; row++)
{
max = list3[row];
for(column =(row-1); column >= 0 ; column--)
{
Thread.Sleep(delay);
tbResult_Insertion.Clear();
for(int i=0; i<list3.Length; i++)
tbResult_Insertion.AppendText(list3[i] + " " );
if(list3[column]>max)
list3[column+1] = list3[column];
else
break;
}
list3[column+1]=max;
display(list3);
}
end = new TimeSpan(System.DateTime.Now.Ticks);
lbEnd_Insertion.Text = end.ToString();
lbTotal_Insertion.Text = end.Subtract(start).ToString();
tbResult_Insertion.Clear();
for(int i=0; i<list3.Length; i++)
tbResult_Insertion.AppendText(list3[i] + " " );
}
public void swap(int a, int b, int aCount, int bCount)
{
int temp =0;
temp = list1[aCount];
list1[aCount] = list1[bCount];
list1[bCount] = temp;
}
private void btBubble_Click(object sender, System.EventArgs e)
{
if(!mode.Equals("race"))
{
textBox1.AppendText("\n\nBUBBLE SORTING\n");
textBox1.AppendText("---------------------\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list1[i]+ " ");
}
bubbleSort();
}else
MessageBox.Show("Click GENERATE to start with a new list.");
}
private void btSelection_Click(object sender, System.EventArgs e)
{
if(!mode.Equals("race"))
{
textBox1.AppendText("\n\nSELECTION SORTING\n");
textBox1.AppendText("---------------------\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list2[i]+ " ");
}
selectionSort();
}
else
MessageBox.Show("Click GENERATE to start with a new list.");
}
private void btInsertion_Click(object sender, System.EventArgs e)
{
if(!mode.Equals("race"))
{
textBox1.AppendText("\n\nINSERTION SORTING\n");
textBox1.AppendText("---------------------\n");
for(int i=0; i<total; i++)
{
textBox1.AppendText(list3[i]+ " ");
}
insertionSort();
}
else
MessageBox.Show("Click GENERATE to start with a new list.");
}
private void btClear_Click(object sender, System.EventArgs e)
{
clearAll();
}
private void clearAll()
{
this.tbResult_Bubble.Clear();
this.tbResult_Insertion.Clear();
this.tbResult_Selection.Clear();
this.lbStart_Bubble.Text = " ";
this.lbStart_Insertion.Text = " ";
this.lbStart_Selection.Text = " ";
this.lbEnd_Bubble.Text = " ";
this.lbEnd_Insertion.Text = " ";
this.lbEnd_Selection.Text = " ";
this.lbTotal_Bubble.Text = " ";
this.lbTotal_Insertion.Text = " ";
this.lbTotal_Selection.Text = " ";
textBox1.Clear();
}
private void display(int[] listX)
{
if(!mode.Equals("race"))
{
textBox1.AppendText("\n");
for(int i=0; i<total; i++)
textBox1.AppendText(" " + listX[i]);
}
}
private void btClearDisplay_Click(object sender, System.EventArgs e)
{
textBox1.Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -