Question

Problem = solved, thank you all!

me and my mate are working on a program to sort an x number of numbers that the user inputs by themselves. This is our progress. The program just wont the way we want it to and we have checked for hours on the internet for solutions and none seem to work. Please help us fix the code. Its a "bubble sort program" if I have understood things corectly.

Also, we are both very new to c# so if possible please dont use complex solutions. Simply try to modify our code with the functions we are currently using. Thank you!

public partial class Form1 : Form
{
    List<int> nummerlista = new List<int>();

    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (input.Text != "")
        {

            int siffra = Convert.ToInt32(input.Text);

            nummerlista.Add(siffra);
            //   nummerlista.Add(Convert.ToInt32(input.Text));

            System.Threading.Thread.Sleep(300);

            input.Clear();
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < nummerlista.Count; i++)
        {
            output.AppendText(Convert.ToString(nummerlista[i]) + " ");
        }
        int t = 0;
        for (int v = 0; v < nummerlista.Count; v++)
        {
            for (int c = 0; c < nummerlista.Count; c++)
            {
                if (nummerlista[v] < nummerlista[c])
                {
                    t = nummerlista[v];

                    nummerlista[v] = nummerlista[c];

                    nummerlista[c] = t;
                }
            }
        }
        for (int i = 0; i < nummerlista.Count - 1; i++)
        {
            outputSorterad.AppendText(Convert.ToString(nummerlista[i]) + " ");
        }
    }
}
Was it helpful?

Solution

You have mistake just in dumping output

for (int i = 0; i < nummerlista.Count - 1; i++)

should be

for (int i = 0; i < nummerlista.Count; i++)

OTHER TIPS

Since your new to C# you need to understand that most of the time the .Net Framework will give you these basic functionality - you just need to find it.

So for sorting you just need to call the sort method against the generic list nummerlista.Sort() http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx

And heres an example of c# bubble sort if the point of this queston is to implement the algorithm https://stackoverflow.com/a/14768087/81053

A couple of suggestions:

When you print you need to dump entire list:

for (int i = 0; i < nummerlista.Count; i++)

When you sort inner loop doesn't need to go to the end of the list. Going to the outer counter is enough:

for (int v = 0; v < nummerlista.Count; v++)
        {
            for (int c = 0; c < v; c++)

Also, accessing elements of the List<> is a bit slow. Probably the better idea is to convert the list to array as soon as input is done.

And for the end, if you don't want to practise different sorting algorithms you can just use List.Sort - http://msdn.microsoft.com/en-us/library/3da4abas(v=vs.110).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top