Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
        }
    }
share|improve this question
2  
what is your code supposed to do? Isn't x always equals to a ? – Steve B Sep 18 '12 at 9:21
1  
x is completely useless. You can use a at its place instead – Daniel Hilgarth Sep 18 '12 at 9:22
I want to create a program that is a small as possible I really hope you talking about code lines not about app size... – Reniuz Sep 18 '12 at 9:23
This code is supposed to group an array which i have sortet before by Bubblesort. If it helps i can show you the whole code.Yep about code lines – user1676819 Sep 18 '12 at 9:25
More suitable for Code Review. – codesparkle Sep 18 '12 at 9:27
show 3 more comments

migrated from stackoverflow.com Sep 18 '12 at 12:16

2 Answers

I'm not familiar with the language you're using (German?), so I didn't spend much time looking at your 'full code'. So, let's just look at the Sortierung() method and the relevant variables.

First, I'm going to refactor it a little:

var values = new int[101]; // contains random integers, 0 <= i < 100 (from other code)
var counts = new int[100];
for (var i = 0; i < values.Length; i++) {              // outer loop
    for (var num = 0; num < values.Length; num++) {    // inner loop
        if (values[i] == num)                          // line 4
            counts[num]++;                             // line 5
    }
}

I've replaced x with a, renamed the variables with more meaningful names, and changed the nesting of the loop. Study this to see why none of these changes affect the functionality of the method. Now, let's analyze what the method does.

In the outer loop, i takes the value of every integer starting at 0 and less than values.Length = 101, i.e. 0~100, inclusively. This is also true for num in the inner loop.

The outer loop tells us that in line 4, every value in values is compared to num.

The inner loop tells us that in line 4, num is a number 0~100, inclusively.

So every value[i] is compared to every possible value of num. Since all values[i] must be 0~99 inclusively and num cycles through 0~100 once for every value[i], line 4 is true once for every value[i], and counts[num] is incremented.

When counts[num] is incremented, values[i] == num (because line 4 was true). That means we count one num every time we find one of the random integers in values is equal to it.

In other words, after the method is done running:

  • counts[0] = the number of 0's in values
  • counts[1] = the number of 1's in values
  • counts[2] = the number of 2's in values
  • ... etc
  • counts[99] = the number of 99's in values

Understanding this, we can refactor again.

Set everything up:

var size = 101
var maxInt = 100;
var rand = new Random();
var values = new List<int>();
for (var i = 0; i < size; i++)                   // do 'size' times
    values.Add(rand.Next(maxInt));               // add a random int 0~99, inclusive
values.Sort();
var counts = new int[maxInt];

And finally, the method:

for (var c = 0; c < c.Length; c++)               // for every value c from 0~99 inclusive
    counts[c] = values.Count(val => val == c);   // count how many 'val' in values equal c
                                                 // and set counts[c] equal to it
share|improve this answer

What do you think about this code of function Sortierung?

private void Sortierung()
{
    for (int a = 0; a < value.Length * value.Length; a++)   
        if (value[a % value.Length] == a / value.Length)
            sortierung[a / value.Length]++;

    ausgabe();
    Sort.Visible = false;
}
share|improve this answer
inefficient to use modulo – Woot4Moo Sep 24 '12 at 19:04
I'm pretty sure this change completely alters what the method actually does. – Kache Sep 25 '12 at 5:54
@Kache, please explain your opinion. What's wrong if compare with function Sortierung in question? – KvanTTT Sep 25 '12 at 7:01
Ah, I am mistaken. At first glance, I wasn't able to discern exactly what it does. I also ran it a few times to check, and it appears to do the same thing. Why don't you remove the "(not tested)" in the post? – Kache Sep 25 '12 at 12:17
Actually i did not test this code, just rewrite it more optimal in answer. Ok, i'll remove it if you've tested it. – KvanTTT Sep 25 '12 at 12:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.