bubble sort logic error

Posted by Arianule on Stack Overflow See other posts from Stack Overflow or by Arianule
Published on 2011-10-20T18:58:39Z Indexed on 2011/11/18 1:50 UTC
Read the original article Hit count: 109

Filed under:

I was trying a basic sorting exercise and I was hoping I could receive some help with what is probably a basic logic error.

int[] numbers = new int[] { 2, 5, 11, 38, 24, 6, 9, 0, 83, 7 };
        for (int loop = 0; loop < numbers.Length; loop++)
        {
            Console.WriteLine(numbers[loop]);
        }
        Console.WriteLine("Performing a bubble sort");

        bool flag = false;
        do
        {

            for (int loop = 0; loop < numbers.Length - 1; loop++)
            {
                if (numbers[loop] > numbers[loop + 1])
                {

                    int temporary = numbers[loop];
                    numbers[loop] = numbers[loop + 1];
                    numbers[loop + 1] = temporary;
                    flag = true;
                }

            }
        } while (flag == false);

        for (int loop = 0; loop < numbers.Length; loop++)
        {
            Console.WriteLine(numbers[loop]);
        }

kind regards

arianule

© Stack Overflow or respective owner

Related posts about c#