-1

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]) + " ");
        }
    }
}
Burmeister
  • 57
  • 1
  • 7
  • 1
    Can't you use the out of the box sorting methods? Or is it some kind of fundamental reasearch? – Steve B Mar 31 '14 at 08:56
  • 2
    Apart from the above.. What isnt working? – Sayse Mar 31 '14 at 08:59
  • 1
    No we can't, as you stated this is a fundamental research project for school and in this program we use just about every function we have learned to this date. So its not and option to use a function like "nummerlista.Sort" if that exists. Thank you! – Burmeister Mar 31 '14 at 09:00
  • Although bubble sort is lame O(n^2) and built-in methods like Sort() are using quicksort - which is O(n*log(n)), this method should work (sort the nummerlista from lowest to biggest). Can you give example of set that isn't being sorted properly ? – Ondrej Svejdar Mar 31 '14 at 09:01
  • @Sayse What happens when we run the program and inputs "9 7 5 3 7 3 8 3" is that we get the output -> "3 3 3 5 7 7 8" which is incorrect. – Burmeister Mar 31 '14 at 09:02
  • So its missing off numbers? .. – Sayse Mar 31 '14 at 09:05
  • http://www.c-sharpcorner.com/UploadFile/3d39b4/bubble-sort-in-C-Sharp/ – ray Mar 31 '14 at 09:06
  • 1
    If you solved your problem, please mark the answer you helped you to solve the issue. If you found the solution yourself, answer to your own question. This will mark the post as answered and help future readers of the question. – Steve B Mar 31 '14 at 11:45
  • @SteveB, OP doesnt seem to grasp the checkmark feature yet. I answered a question for him and its been hanging. I'm sure its just a newbie thing, hopefully he'll see this and get around to it. – crthompson Apr 10 '14 at 22:47

3 Answers3

5

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++)
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
0

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

Community
  • 1
  • 1
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
0

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

Klark
  • 8,162
  • 3
  • 37
  • 61