我正在使用random
和sorting
方法。到目前为止,我有线性方法工作。我正在尝试实现一种方法,让用户决定使用哪种类型的排序,例如linear
、bubble
、index
等。所以我添加textBox7
了标签Key
,button3
用户点击它,直到找到他们想要的排序方法。当 Keytextbox7
选择了一个值时,排序数组button2
将执行该特定的排序方法。如何根据Key
值执行特定的排序方法?下面是我如何设置 windows 窗体的图片
代码
namespace sortmachine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch sw = new Stopwatch();
Random r = new Random();
private int size = 0;
private int max = 0;
private int ops = 0;
private int[] ar1;
private int[] ar2;
private int[] ar3;
private void button1_Click(object sender, EventArgs e)
{
try
{
size = Convert.ToInt32(textBox2.Text);
max = Convert.ToInt32(textBox3.Text);
string s = "";
ar1 = new int[size];
ar2 = new int[size];
ar3 = new int[size];
for (int i = 0; i < size; i++)
{
int n = r.Next(0, max);
ar1[i] = n;
ar2[i] = ar1[i];
}
for (int i = 0; i < size; i++)
{
s += ar1[i].ToString() + " ";
}
listView1.Items.Add(s);
}
catch
{
MessageBox.Show("Please input information");
}
}
private void button2_Click(object sender, EventArgs e)
{
int min = max;
int n = 0;
string s="";
sw.Start();
for (int j = 0; j < size; j++)
{
for (int i = 0; i < size; i++)
{
if (min > ar2[i])
{
min = ar2[i];
n = i;
ops++;
}
if (min < ar2[i])
{
ops++;
}
}
ar2[n]=max;
ar3[j] = min;
min = max;
s += ar3[j] + " ";
}
TimeSpan x = sw.Elapsed;
textBox5.Text = x.ToString();
listView2.Items.Add(s);
listView2.Text = s;
textBox6.Text = Convert.ToString(ops);
if (checkBox1.Checked==true)
{
textBox1.AppendText(s+Environment.NewLine);
}
for (int i = 0; i < size; i++)
{
ar2[i] = ar1[i];
}
ops = 0;
}
}
}