Arianule
Arianule

Reputation: 9053

Logic behind a bubble sort

I am doing a bubble sort exercise and my feeling is that it is very close to correct. As it is at the moment I am presented with an eternal loop.

Where does the fault lie ?

static void Main(string[] args)
    {
        int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 };
        int temporary;
        bool sorted;
        do
        {
            sorted = false;

            for (int i = 0; i < numbers.Length - 1; i++)
            {
                int a = numbers[i];
                int b = numbers[i + 1];
                if (a > b)
                {
                    temporary = a;
                    a = b;
                    b = temporary;

                    sorted = true;

                }
            }
            Console.WriteLine("sorted");
        } while (sorted == true);


        foreach (int i in numbers)
        {
            Console.Write(i + " ");
        }

    }

Upvotes: 2

Views: 1207

Answers (5)

MoonKnight
MoonKnight

Reputation: 23831

A better approach in C# is to use a generic bubble sort

public void BubbleSort<T>(IList<T> list);
{
    BubbleSort<T>(list, Comparer<T>.Default);
}

public void BubbleSortImproved<T>(IList<T> list, IComparer<T> comparer)
{
    bool stillGoing = true;
    int k = 0;
    while (stillGoing)
    {
        stillGoing = false;
        for (int i = 0; i < list.Count - 1 - k; i++)
        {
            T x = list[i];
            T y = list[i + 1];
            if (comparer.Compare(x, y) > 0)
            {
                list[i] = y;
                list[i + 1] = x;
                stillGoing = true;
            }
        }
        k++;
    }
}

A brief explanation of this algorithm is given by Jon Skeet in his answer here. "It uses an arbitrary comparer, but lets you omit it in which case the default comparer is used for the relevant type. It will sort any (non-readonly) implementation of IList, which includes arrays."

I hope this helps.

Upvotes: 4

Alex Filipovici
Alex Filipovici

Reputation: 32571

Here is an working example:

static void Main(string[] args)
{
    int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 };
    int temporary;
    bool sorted;

    do
    {
        sorted = false;
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            if (numbers[i] > numbers[i + 1])
            {

                temporary = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = temporary;

                sorted = true;
            }
        }
        Console.WriteLine("sorted");
    } while (sorted == true);

    foreach (int i in numbers)
    {
        Console.Write(i + " ");
    }
}

Upvotes: 1

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

You exchange a with b but you DON'T do anything to your input array. Thus, you are constantly exchanging values in memory but the original array is not changed. Try:

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

                    sorted = true;

                }
            }

Upvotes: 3

Anirudha
Anirudha

Reputation: 32807

It should be

if (numbers[i] > numbers[i + 1])
    {
            temporary = numbers[i];
            numbers[i] = numbers[i + 1];
            numbers[i + 1] = temporary;

            sorted = true;

     }

changes made in a,b doesn't reflect in numbers[i] numbers[i+1] because a and b are mere copy of numbers[i] numbers[i+1]..

Upvotes: 1

rro
rro

Reputation: 619

You are not writing the results back onto the array.

Use this instead:

//temporary = a;
//a = b;
//b = temporary;

numbers[i] = b;
numbers[i + 1] = a;

Upvotes: 2

Related Questions