I am working with random and sorting methods. So far I have the linear method working. I am trying to implement a way that will let the user decide which type of sorting to use such as linear, bubble, index, etc. So I have added textBox7 with the label Key and button3 which the user clicks until the find their desired sorting method. When Keytextbox7 has a value chosen then the sort array button2 will execute that particular sorting method. How can I execute a particular sorting method based on the Key value? Below is picture of how I have the windows form set up
CODE
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;
}
}
}
Id give this a go as it implements what others were suggesting:
Inside each if conditional you can perform your own logic per sort type. As you can see I have a sorting option selected from the combobox with other buttons to add numbers to my list for sorting and a control to display the results.
The code is as follows in the Screenshot: