-4

I am trying to make a bubble sort program that can sort array of integers from bottom.

var list = new int[] {5,0,2}; //array

for (int i = 0; i < list.Length; i++)
{
    while (list[i] > list[i+1])
    {
        list[i] = list[i + 1];
    }
}
Console.WriteLine(string.Join(",", list));

I get the index out of range error in the while (list[i] > list[i+1]). What is wrong with my code? Is my while condition bad?

Arnye Mob
  • 3
  • 3
  • The valid array indices are 0 to `list.length-1`. Your `list[i+1]` goes out of bounds. You should also avoid generic names list `list`. List of what? Call it something list `listOfXxx` where `Xxx` is something meaningful. Like, `listOfScores`... – lurker May 04 '18 at 11:39
  • inside the loop the maximum value of i is the upper index of the array, when you access i+1 you exceed this. – Alex K. May 04 '18 at 11:39
  • 2
    As an aside, I'd probably rename `list` to `array` given that it's *not* a list :) – Jon Skeet May 04 '18 at 11:39
  • 1
    One more thing regarding bubble sort ... it runs till there are no swaps. I don't see this condition in your code. – Dimitar May 04 '18 at 11:44
  • yes Alex sorry for that, in our group we call arrays and lists both lists. truly bad habit. :D now the program works but always deletes one number to 0, gues i will have to rewrite it. – Arnye Mob May 04 '18 at 11:45
  • You can find the code for bubble sort here [Simple bubble sort c#](https://stackoverflow.com/questions/14768010/simple-bubble-sort-c-sharp) – Slaven Tojić May 04 '18 at 11:46
  • Thx Slaven, i will check it out :) – Arnye Mob May 04 '18 at 11:51

2 Answers2

4

is my while condition bad?

No, your for loop condition is incorrect. Look at the loop here and consider what that says about the value of i within the body of the loop.

for (int i = 0; i < list.Length; i++)

That ensures that list[i] is always valid. But you're using list[i + 1], so you need to make sure that i + 1 is a valid index into the array. The simplest way to do that is to just reduce the limit in the condition:

for (int i = 0; i < list.Length - 1; i++)

That will remove the exception, but you'll be left with this while loop:

while (list[i] > list[i+1])
{
    list[i] = list[i + 1];
}

You don't modify the value of i within the loop, which means it's only ever going to execute once - as soon as you've executed the body of that loop, the condition will become false. So it would be clearer to write it as an if statement:

if (list[i] > list[i+1])
{
    list[i] = list[i + 1];
}

Now I suspect that's not really what you want - you probably want to swap values rather than just assigning, and you probably want a single loop, but that's rather beyond the scope of the immediate question.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thx this works, however i lose one number in the process, i will have to rewrite it – Arnye Mob May 04 '18 at 11:46
  • @ArnyeMob: I'm not sure what you mean by "lose", but there are certainly other problems with the code, yes. (Typically in a sort you *swap* elements rather than doing a simple copy.) I tried to just explain the specific error you've currently got and give some indication of what to think about next. You may well want to use pen and paper or a whiteboard to try to work out what the code *should* do - then implement it. – Jon Skeet May 04 '18 at 11:48
  • Daisy Shipton i rewrote the inner while loop to: int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; now i must add repetition to this so i can check the list more than once. thanks for your help so far – Arnye Mob May 04 '18 at 11:59
0

Thx everyone for help, finally i made it work with adding a bool that guarantees checking the array if it is needed to repeat inner loop again:

        var list = new int[] {5,7,0,2}; //array

        bool alert = true;

        while (alert == true)
        {
            alert = false;
            for (int i = 0; i < list.Length - 1; i++)
            {
                while (list[i] > list[i + 1])
                {
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
                    alert = true;
                }
            }
        }
        Console.WriteLine(string.Join(",", list));
        Console.ReadKey();
Arnye Mob
  • 3
  • 3