I have a little problem. I want to create this process that is a small as possible. The following code I have coded now:
private void Sortierung()
{
int x = -1;
for (int a = 0; a < value.Length; a++)
{
x++;
for (int b = 0; b < value.Length; b++) /
{
if (value[b] == x)
{
sortierung[a]++;
}
}
}
ausgabe();
Sort.Visible = false;
}
Now I thought it must be possible to do this in a single loop and not in two loops, because the array is already sorted (it s array with 100 random ints)
int x = -1; // x ist minus 1
for (int a = 0; a < 100; a++) // das ganz wiederholt sich 100 mal
{
x++;
if (value[a] <= value[a + 1])
{
if (value[a] == x)
{
sortierung[a]++;
}
}
}
Can somebody help me and show me my mistake? Sorry if this question seems stupid, but I'm learning C# for fun atm and the book I have is not helpful in this issue.
Thanks in advance
full code:
public partial class Form1 : Form { const int bereich = 100; int[] value = new int[101]; int zuweiser; bool aufsteigend; int[] sortierung = new int[bereich]; public Form1() { InitializeComponent(); vorbereitung(); } private void button1Click(object sender, EventArgs e) { Focus(); vorbereitung(); textBox1.Text = string.Empty; Random rnd = new Random(); for (zuweiser = 0; zuweiser < value.Length; zuweiser++) { value[zuweiser] = rnd.Next(bereich); } Bubblesort(); foreach (int inhalt in value) { textBox1.Text += inhalt + ", "; } Sort.Visible = true; if (textBox1.Text != string.Empty) { Sort.Focus(); } } private void Bubblesort() { if (checkBox1.Checked == true) { aufsteigend = true; } else { aufsteigend = false; } int zaehler = 0; for (int a = 0; a < value.Length; a++) { zaehler++; for (int b = a + 1; b < value.Length; b++) { if (aufsteigend == false) { zaehler++; if (value[a] > value[b]) { int temp = value[b]; value[b] = value[a]; value[a] = temp; } } else { if (value[a] < value[b]) { int temp = value[b]; value[b] = value[a]; value[a] = temp; } } } } } private void SortClick(object sender, EventArgs e) { zaehlung(); } private void zaehlung() { int x = -1; // x ist minus 1 for (int a = 0; a < 100; a++) // das ganz wiederholt sich 100 mal { x++; if (value[a] <= value[a + 1]) { if (value[a] == x) { sortierung[a]++; } } } ausgabe(); } private void Sortierung() { int x = -1; // x ist minus eins for (int a = 0; a < value.Length; a++) // das ganze passiert 100 mal { x++; // x wird hochgezählt ist also 0 for (int b = 0; b < value.Length; b++) // jede zahl in value abfragen ob sie x ist { if (value[b] == x) { sortierung[a]++; //wenn eine zahl x ist in sortierung[a] hochzählen } } } ausgabe(); Sort.Visible = false; } private void ausgabe() { textBox1.Text = string.Empty; int g= -1; foreach (int i in sortierung) { g++; textBox1.Text += ("Zahl: " + test[g].ToString() + " Anzahl: " + i.ToString() + "\r\n"); } foreach (int j in sortierung) { sortierung[j] = 0; } } private void vorbereitung() { Output1.Text = string.Empty; Output2.Text = string.Empty; } private void textBox1KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar > 0) { e.Handled = true; } }
xalways equals toa? – Steve B Sep 18 '12 at 9:21xis completely useless. You can useaat its place instead – Daniel Hilgarth Sep 18 '12 at 9:22I want to create a program that is a small as possibleI really hope you talking about code lines not about app size... – Reniuz Sep 18 '12 at 9:23