Search code examples
c#sortingbubble-sort

Bubble sort in c# using windows forms. Newbies asking


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]) + " ");
        }
    }
}

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++)